How to Make a Dictionary from Two Lists in Python with dict and zip
You have the keys in one list and the values in another, in the same order, and you want a dictionary so you can look a value up by its key. dict(zip(keys, values)) does it in one line. This guide builds a small price list on Python 3.15.2.
Syntax
<name> = dict(zip(<keys>, <values>))
| Part | What it means |
|---|---|
zip(<keys>, <values>) | Pairs the first key with the first value, the second with the second, and so on |
dict(...) | Builds a dictionary from those pairs: the first item of each pair becomes the key, the second the value |
The Python documentation uses this exact form as one of its ways to make a dictionary, dict(zip(['one', 'two', 'three'], [1, 2, 3])), and explains the rule behind it: "Each item in the iterable must itself be an iterable with exactly two elements. The first element of each item becomes a key in the new dictionary, and the second element the corresponding value."
The Example
A list of fruit names and a list of their prices become one dictionary. The script prints it and looks up the price of a banana.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
fruits = ["apple", "banana", "cherry"]
prices = [1.2, 0.5, 3.0]
price_of = dict(zip(fruits, prices))
print(price_of)
print(price_of["banana"])

Step 2: Run It
uv run python prices.py
{'apple': 1.2, 'banana': 0.5, 'cherry': 3.0}
0.5

Each fruit became a key and its price the value, in the order of the lists. price_of["banana"] found 0.5 by name, without searching a list. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| The same key appears twice in the keys list | Only the last value is kept: dict(zip(["a", "a"], [1, 2])) gave {'a': 2}. The docs: "If a key occurs more than once, the last value for that key becomes the corresponding value" | Make sure the keys are unique before you build the dictionary |
| The two lists have different lengths | zip stops at the shorter one, so some keys or values are dropped without a word | Use zip(keys, values, strict=True) to get an error instead |
| Looking up a key that isn't there | KeyError: 'b' | Check with in first, or use .get(key) |
FAQ
Can I swap keys and values?
Yes, swap the arguments: dict(zip(prices, fruits)). The values then need to be unique, or later ones replace earlier ones.
Does the dictionary keep the order of the lists?
Yes. Dictionaries keep the order in which keys were added, and zip adds them in list order.
Conclusion
dict(zip(fruits, prices)) turned two matching lists into {'apple': 1.2, 'banana': 0.5, 'cherry': 3.0}, and price_of["banana"] returned 0.5.
