zip()

Walking two or more sequences together, and the silent truncation when they are not the same length.

Overview

The basic shape

for name, score in zip(names, scores):

Each pass gives you the next item from each list. The alternative is for i in range(len(names)) followed by two lookups, which works and puts an index between you and the values.

It takes any number of iterables:

for name, score, grade in zip(names, scores, grades):

zip.py

zip.py Python 3
Output

                    

zip_uneven.py

zip_uneven.py Python 3
Output

                    

Worth knowing

zip stops at the shortest input and says nothing about it.
strict=True raises on a length mismatch (Python 3.10+).
dict(zip(keys, values)) is the standard way to build a dict from two lists.
zip(*pairs) unzips, and hands back tuples.

zip(): A Practical Guide

zip walks several sequences in step, yielding one tuple per position. It is the clean answer to "I have two lists that line up" — with one behaviour worth knowing before it bites.

Building a dict

dict(zip(keys, values))

This is the standard idiom for turning two parallel lists into a mapping, and it is worth recognising on sight because it appears everywhere.

It truncates, and it does not tell you

This is the part that costs people an afternoon:

zip(["ana", "bo", "cy", "dee"], [91, 78])

gives two pairs. cy and dee are gone. No error, no warning — zip stops when the shortest input runs out, by design.

When the lists come from the same source that is usually harmless. When one is data and the other is a lookup that quietly returned fewer rows, you get a silently shortened result, which is the worst kind of wrong.

Two ways to be explicit:

zip(a, b, strict=True)  # raises ValueError on mismatch (3.10+)
zip_longest(a, b, fillvalue=0)  # pads instead, from itertools

If the lists are supposed to be the same length, strict=True turns a silent bug into an immediate error. That is nearly always the better trade.

Unzipping

The same function reverses itself with a star:

who, what = zip(*pairs)

zip(*pairs) spreads the list of pairs into arguments, so zip receives each pair as a separate iterable and re-pairs them by position. The results come back as tuples, not lists — wrap in list() if that matters.

It is lazy

zip returns an iterator, not a list. It produces pairs as they are asked for, which is what lets it work on files and generators. It also means you can only walk it once: consume it in a loop and it is exhausted. list(zip(...)) when you need to keep the result.

Check yourself

0 of 3

Answer without scrolling back up.

  1. `zip(['a','b','c'], [1,2])` produces how many pairs?

  2. How do you make a length mismatch an error?

  3. What does `zip(*pairs)` do?

Cheat sheet

zip()

zip walks several sequences in step, yielding one tuple per position. It is the clean answer to "I have two lists that line up" — with one behaviour worth knowing before it bites.

PYTHON · vizlearn.in/python/zip_function.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.