Skip to main content

How to Make a Dictionary from Two Lists in Python with dict and zip

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<name> = dict(zip(<keys>, <values>))
PartWhat 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​

prices.py
fruits = ["apple", "banana", "cherry"]
prices = [1.2, 0.5, 3.0]

price_of = dict(zip(fruits, prices))
print(price_of)
print(price_of["banana"])

The prices.py script in a macOS-style code window: a list of three fruits, a list of three prices, dict(zip(fruits, prices)) stored as price_of, then the whole dictionary and the banana price printed.

Step 2: Run It​

Terminal
uv run python prices.py
Output
{'apple': 1.2, 'banana': 0.5, 'cherry': 3.0}
0.5

The terminal after uv run python prices.py: the dictionary with apple 1.2, banana 0.5 and cherry 3.0, then 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​

MistakeWhat happensDo this instead
The same key appears twice in the keys listOnly 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 lengthszip stops at the shorter one, so some keys or values are dropped without a wordUse zip(keys, values, strict=True) to get an error instead
Looking up a key that isn't thereKeyError: '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.

Additional Resources​

  • dict — every way to create a dictionary.
  • zip() — how the pairs are made.