How to Create a Dict from Two Parallel Lists in Python (zip)

Build a dictionary by pairing elements from two parallel lists using Python's built-in zip function and dict constructor.

Easy Python 3.9+ Aug 9, 2026 Dictionaries & sets 13 views 0 copies

Python code

5 lines
Python 3.9+
keys = ["name", "age", "city"]
values = ["Alice", 30, "New York"]

result = dict(zip(keys, values))
print(result)

Output

stdout
{'name': 'Alice', 'age': 30, 'city': 'New York'}

How it works

The zip function pairs the first element of keys with the first element of values, the second with the second, and so on, producing an iterator of tuples. The dict() constructor consumes these tuples and converts each tuple (key, value) into a dictionary entry. This works because each tuple has exactly two elements — the key and the value. If the lists are of unequal length, zip stops at the shorter one, which silently drops extra elements — be aware of that behavior. For most everyday use, this one-liner is the cleanest way to merge two aligned lists into a mapping.

Common mistakes

  • Forgetting that zip stops at the shorter list, silently discarding extra elements
  • Assuming order is preserved (it is, but rely on insertion order carefully when keys are not unique)
  • Using two separate loops when zip can do it in one line

Variations

  1. Use a dictionary comprehension: {k: v for k, v in zip(keys, values)}
  2. Convert zip output to a list then to dict: dict(list(zip(keys, values)))

Real-world use cases

  • Mapping CSV column headers (as keys) to a row of cell values in data processing.
  • Combining an API response's field names with their corresponding values to form a JSON-ready dict.
  • Pairing configuration keys with user-provided settings loaded from parallel input arrays.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Dictionaries & sets

Related tutorials and quizzes for this topic.