Skip to main content

How to Loop over Two Lists Together in Python with zip

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

When two lists line up, such as names in one and ages in another, you often need the first name with the first age, the second with the second, and so on. Python's built-in zip() pairs them up for you. This guide loops over two lists together on Python 3.15.2.

Syntax​

Syntax
for <item_a>, <item_b> in zip(<list_a>, <list_b>):
...
PartWhat it means
zip(<list_a>, <list_b>)Pairs the first item of each list, then the second, and so on
<item_a>, <item_b>The two values of each pair, one from each list

The Python documentation says zip() will "Iterate over several iterables in parallel, producing tuples with an item from each one." It works with more than two lists too.

The Example​

One list holds three names and another holds their ages, in the same order. The script prints each name next to its age.

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​

pairs.py
names = ["Ada", "Grace", "Linus"]
ages = [36, 45, 28]

for name, age in zip(names, ages):
print(name, age)

The pairs.py script in a macOS-style code window: a list of three names, a list of three ages, and a for loop over zip(names, ages) that prints each name with its age.

Step 2: Run It​

Terminal
uv run python pairs.py
Output
Ada 36
Grace 45
Linus 28

The terminal after uv run python pairs.py: Ada 36, Grace 45 and Linus 28, each on its own line.

Each turn of the loop, zip handed over one tuple, such as ("Ada", 36), and the loop unpacked it into name and age. The command works unchanged in PowerShell.

When the Lists Have Different Lengths​

zip stops at the end of the shortest list. The documentation says: "By default, zip() stops when the shortest iterable is exhausted. It will ignore the remaining items in the longer iterables". If ages had only two numbers, Linus would be skipped without any warning. Pass strict=True to get an error instead when that would be a bug.

Common Mistakes​

MistakeWhat happensDo this instead
Printing zip(names, ages) to see the pairsYou get <zip object at 0x…>, because zip is lazy: "The elements won't be processed until the iterable is iterated on"Loop over it, or wrap it in list(): list(zip(names, ages))
Lists of different lengths by accidentThe extra items are dropped silentlyUse zip(names, ages, strict=True)

FAQ​

Can I zip three lists?​

Yes. zip([1, 2], "ab", [True, False]) gave [(1, 'a', True), (2, 'b', False)] once wrapped in list(). Use three names in the loop: for number, letter, flag in zip(...).

Why not use a counter and names[i]?​

You can, but zip says what you mean in one line, and it works with things that have no positions, such as files read line by line.

Conclusion​

for name, age in zip(names, ages) printed each name with its age. zip pairs items by position and stops at the shortest list.

Additional Resources​

  • zip() — the built-in function, with strict and examples.
  • Looping techniques — the Python tutorial's tips, including zip.