Glossary

Zip

zip is a built-in function that takes two or more iterables and returns an iterator of tuples, where the i-th tuple contains the i-th element from each iterable. For example, zip([1, 2, 3], ['a', 'b', 'c']) produces [(1, 'a'), (2, 'b'), (3, 'c')]. zip stops at the shortest iterable; use itertools.zip_longest() to pad shorter iterables with a fill value.

zip is commonly used to iterate over parallel sequences (for name, score in zip(names, scores):), to create dictionaries from two lists (dict(zip(keys, values))), and to transpose a matrix (list(zip(*matrix))). Python 3.10 added a strict parameter: zip(a, b, strict=True) raises ValueError if the iterables have different lengths, catching a common source of bugs.

Like enumerate, zip is lazy — it produces tuples on demand without materialising the entire result. This makes it memory-efficient for large iterables. zip is one of Python's fundamental iterator-combining tools and appears frequently in idiomatic Python code. It pairs naturally with tuple unpacking in for loops, creating clean, readable iterations over corresponding elements.

Related terms: Enumerate, Iterator, Tuple

Discussed in:

This site is currently in Beta. Please email Chris Paton (cpaton@gmail.com) with any suggestions, questions or comments.