Skip to main content
Crowdfunding
Python + AI for Geeks
Practice

Create Dictionary from Key/Value Pairs - Solution

Explore 3 ways to create a dictionary from key/value pairs.


Method 1
def solution(keys, values):
result = {}
for i in range(len(keys)): # Use index to retrieve each key and value.
result[keys[i]] = values[i] # Add key and value to the dictionary.
return result

By using the code above, you can merge the input key list and value list to create a dictionary.


Example Usage

Input/Output Example
keys = ['a', 'b', 'c']
values = [1, 2, 3]

result = solution(keys, values)
print(result) # Output: {'a': 1, 'b': 2, 'c': 3}

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.