Create Object from Two Arrays — LeetCode 2794 Python Solution

EasyLeetCode PremiumJavaScript
Problem
#2794
Reading time
2 min

The problem

Given two arrays keysArr and valuesArr, return a new object obj. Each key-value pair in obj should come from keysArr[i] and valuesArr[i].

Example

Input
keysArr = ["a", "b", "c"], valuesArr = [1, 2, 3]
Output
{"a": 1, "b": 2, "c": 3}
Explanation
The keys "a", "b", and "c" are paired with the values 1, 2, and 3 respectively.

Python solution

Python
class Solution:
    def createObject(self, keysArr: List[str], valuesArr: List[int]) -> dict:
        obj = {}
        for k, v in zip(keysArr, valuesArr):
            obj[k] = v
        return obj

Complexity

MeasureComplexity
TimeO(n)
SpaceO(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 2794. Create Object from Two Arrays?
LeetCode 2794. Create Object from Two Arrays is rated Easy on LeetCode.
What is the time complexity of LeetCode 2794. Create Object from Two Arrays?
The Python solution on this page runs in O(n).
What is the space complexity of LeetCode 2794. Create Object from Two Arrays?
The Python solution on this page uses O(n) auxiliary space.
What topics does LeetCode 2794. Create Object from Two Arrays cover?
LeetCode 2794. Create Object from Two Arrays is tagged JavaScript on LeetCode.
Is LeetCode 2794. Create Object from Two Arrays a premium problem?
Yes. LeetCode 2794. Create Object from Two Arrays is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview