How to Loop over Two Lists Together in Python with zip
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
for <item_a>, <item_b> in zip(<list_a>, <list_b>):
...
| Part | What 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
names = ["Ada", "Grace", "Linus"]
ages = [36, 45, 28]
for name, age in zip(names, ages):
print(name, age)

Step 2: Run It
uv run python pairs.py
Ada 36
Grace 45
Linus 28

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
| Mistake | What happens | Do this instead |
|---|---|---|
Printing zip(names, ages) to see the pairs | You 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 accident | The extra items are dropped silently | Use 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
strictand examples. - Looping techniques — the Python tutorial's tips, including
zip.
