EasyNo. 2794LeetCode Premium

Create Object from Two Arrays — LeetCode 2794 Python Solution

Topics
JavaScript
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

PythonSolution
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

Time
O(n)
Space · auxiliary
O(n)

Related problems

Related problems
No.ProblemDifficulty
2785Sort Vowels in a StringMedium
0056Merge IntervalsMedium
0147Insertion Sort ListMedium
0164Maximum GapMedium
0274H-IndexMedium
0406Queue Reconstruction by HeightMedium

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.

Free to download. Check it runs on your machine, then pick a plan.