PythonArraysBeginner

zip()

Iterates over multiple iterables in parallel, pairing items.

Review the syntaxStudy the examplesOpen the coding app
for a, b in zip(iter1, iter2):
    ...

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

Iterates over multiple iterables in parallel, pairing items.

zip() is a built-in Python function that aggregates elements from multiple iterables (such as lists, tuples, or strings) into an iterator of tuples. The i-th tuple contains the i-th element from each of the input arguments. It utilizes lazy evaluation, meaning it generates elements on-the-fly and consumes very little memory even when processing massive datasets. A critical technical detail is the 'shortest wins' behavior: zip() stops as soon as the shortest input iterable is exhausted, which can lead to silent data loss if sequences are of unequal length. Since Python 3.10, the 'strict' parameter can be set to True to raise a ValueError if lengths do not match. Performance-wise, zip() runs in O(N) time where N is the length of the shortest iterable and O(1) auxiliary space beyond the resulting tuples.

Quick reference

Syntax

for a, b in zip(iter1, iter2):
    ...

Inputs

Parameters

iterablesIterable[Any][] · Two or more iterables to combine.

See it in practice

Examples

1

Parallel Iteration and Combination

products = ['Laptop', 'Mouse', 'Keyboard']
prices = [1200, 25, 75]
for item, price in zip(products, prices):
    print(f'Product: {item}, Price: ${price}')
Output:
Product: Laptop, Price: $1200 Product: Mouse, Price: $25 Product: Keyboard, Price: $75

Combines two related lists into pairs for simultaneous iteration in a single loop.

2

Matrix Transposition

matrix = [[1, 2, 3], [4, 5, 6]]
transposed = list(zip(*matrix))
print(transposed)
Output:
[(1, 4), (2, 5), (3, 6)]

Uses the unpacking operator (*) with zip to swap rows and columns of a 2D data structure.

3

Building Dictionaries Dynamically

headers = ['id', 'username', 'email']
user_data = [1001, 'dev_user', 'dev@example.com']
user_dict = dict(zip(headers, user_data))
print(user_dict)
Output:
{'id': 1001, 'username': 'dev_user', 'email': 'dev@example.com'}

Pairs keys and values together and passes the iterator directly to the dict() constructor.

Debug faster

Common Errors

1

LogicError

Cause: The input iterables have different lengths, causing zip() to silently drop trailing data from the longer iterables.

Fix: Use itertools.zip_longest() if you need to preserve all data, or set strict=True (Python 3.10+) to catch length mismatches.

list(zip([1, 2, 3], ['a', 'b']))
2

StopIteration / Exhaustion

Cause: zip() returns an iterator, which can only be traversed once. Attempting to iterate over the same zip object a second time will yield nothing.

Fix: Convert the zip object to a list or tuple if you need to reuse the aggregated data multiple times.

z = zip([1], [2])
list(z)
print(list(z))

Runtime support

Compatibility

Python 3.8+

Source: MDN Web Docs

Common questions

Frequently Asked Questions

Iterates over multiple iterables in parallel, pairing items.

iterables: Two or more iterables to combine.

LogicError: Use itertools.ongest() if you need to preserve all data, or set strict=True (Python 3.10+) to catch length mismatches. StopIteration / Exhaustion: Convert the zip object to a list or tuple if you need to reuse the aggregated data multiple times.