Skip to main content
Practice

lists-to-dictionary

---
id: lists-to-dictionary
title: Create Dictionary from Key/Value Pairs - Solution
description: Write a function to combine given key lists and value lists into a dictionary
tags:
- Python
- Dictionary
- List
sidebar_position: 15
isPublic: false
---

# Create Dictionary from Key/Value Pairs - Solution

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

<br />

```python title="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.