zip()
zip(*iterables, strict=False) Returns:
iterator of tuples · Updated March 13, 2026 · Built-in Functions built-in iteration tuples parallel
The zip() function takes multiple iterables and returns an iterator of tuples. Each tuple contains the corresponding elements from each iterable. This is useful for parallel iteration, creating dictionaries, and processing related data together.
Syntax
zip(*iterables, strict=False)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
*iterables | iterables | — | Two or more iterables to combine. Can be lists, tuples, strings, or any iterable. |
strict | bool | False | If True, raises ValueError if the iterables have different lengths. Available in Python 3.10+. |
Examples
Basic parallel iteration
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
for name, age in zip(names, ages):
print(f"{name} is {age} years old")
# Alice is 25 years old
# Bob is 30 years old
# Charlie is 35 years old
Creating a dictionary
keys = ["name", "age", "city"]
values = ["Alice", 25, "London"]
person = dict(zip(keys, values))
print(person)
# {'name': 'Alice', 'age': 25, 'city': 'London'}
Using strict=True (Python 3.10+)
list1 = [1, 2, 3]
list2 = [4, 5]
# Without strict (stops at shortest)
result = list(zip(list1, list2))
print(result)
# [(1, 4), (2, 5)]
# With strict=True (raises ValueError)
try:
result = list(zip(list1, list2, strict=True))
except ValueError as e:
print(f"Error: {e}")
# Error: zip() argument 2 is shorter
Common Patterns
Converting two lists to a dict
columns = ["id", "name", "score"]
rows = [1, 2, 3], ["Alice", "Bob", "Charlie"], [95, 87, 91]
data = [dict(zip(columns, row)) for row in zip(*rows)]
print(data)
# [{'id': 1, 'name': 'Alice', 'score': 95}, {'id': 2, 'name': 'Bob', 'score': 87}, {'id': 3, 'name': 'Charlie', 'score': 91}]
Unzipping with the unpacking operator
pairs = [(1, 4), (2, 5), (3, 6)]
firsts, seconds = zip(*pairs)
print(firsts)
# (1, 2, 3)
print(seconds)
# (4, 5, 6)
Iterating over multiple sequences
x_coords = [1, 2, 3, 4]
y_coords = [10, 20, 30, 40]
points = list(zip(x_coords, y_coords))
print(points)
# [(1, 10), (2, 20), (3, 30), (4, 40)]
When zip() Stops
By default, zip() stops when the shortest iterable is exhausted. If you need all elements from all iterables, use itertools.zip_longest() from the standard library.
See Also
- built-in::enumerate — iterate with indices
- built-in::map — apply a function to elements