Python zip() Function
zip(*iterables, strict=False) combines items from several iterables into tuples, stopping when the shortest one runs out.zip() is the tool for walking two or more sequences side by side: names with scores, keys with values, x-coordinates with y-coordinates. Each loop step receives one tuple holding the matching item from every iterable.
Like enumerate(), zip() returns a lazy iterator. Wrap it in list() or dict() to keep the result. See the lesson on enumerate and zip for more patterns.
| Syntax | zip(*iterables, strict=False) |
|---|---|
| Parameters | *iterables: any number of iterables; strict: if True, raise ValueError when lengths differ (Python 3.10+) |
| Returns | A zip iterator of tuples |
| Default with unequal lengths | Stops silently at the shortest iterable |
| Time complexity | O(1) per tuple, O(n) overall |
How zip() pairs items together
The first tuple holds the first item of every iterable, the second tuple holds the second items, and so on. With two lists of three items you get three pairs.
names = ["Asha", "Ravi", "Meera"]
marks = [88, 92, 79]
print(list(zip(names, marks)))In a loop you normally unpack the tuple: for name, mark in zip(names, marks):.
What zip() does when lengths differ
By default zip() stops as soon as one iterable is exhausted, so extra items in longer ones are dropped without any warning. That is handy when you want it, and a quiet source of bugs when you don't.
Pass strict=True (Python 3.10 and newer) to turn a length mismatch into a ValueError. If you want to keep the longer data and fill the gaps, use itertools.zip_longest, covered in the itertools lesson.
| Call | Lengths 3 and 2 | Result |
|---|---|---|
zip(a, b) | Unequal | Two tuples; the third item of a is dropped |
zip(a, b, strict=True) | Unequal | ValueError while iterating |
zip_longest(a, b) | Unequal | Three tuples, filled with None |
Unzipping with zip(*pairs)
The * operator spreads a list of tuples into separate arguments, and zip() then regroups them by position. This turns a list of pairs back into separate sequences. See unpacking for how the star works.
pairs = [("a", 1), ("b", 2), ("c", 3)]
letters, numbers = zip(*pairs)
print(letters)
print(numbers)Examples you can run
Press Run, then edit the code and run it again.
Loop over two lists together
Each step unpacks one name and one mark.
names = ["Asha", "Ravi", "Meera"]
marks = [88, 92, 79]
for name, mark in zip(names, marks):
print(name, mark)Expected outputAsha 88
Ravi 92
Meera 79
Build a dictionary from two lists
dict(zip(keys, values)) is the standard way to combine them.
fields = ["id", "item", "qty"]
values = [101, "pen", 3]
print(dict(zip(fields, values)))Expected output{'id': 101, 'item': 'pen', 'qty': 3}
Catch mismatched lengths with strict=True
The error is raised while iterating, once one list runs out early.
a = [1, 2, 3]
b = [10, 20]
try:
print(list(zip(a, b, strict=True)))
except ValueError as e:
print(e)Expected outputzip() argument 2 is shorter than argument 1
Compute with paired values
Combine zip() with a comprehension to calculate line totals.
prices = [2.5, 4.0, 1.5]
qty = [4, 2, 10]
totals = [p * q for p, q in zip(prices, qty)]
print(totals)
print(sum(totals))Expected output[10.0, 8.0, 15.0]
33.0
Common mistakes
Reusing an exhausted zip
✗ Wrong
z = zip([1, 2], [3, 4])
print(list(z))
print(list(z))✓ Right
z = list(zip([1, 2], [3, 4]))
print(z)
print(z)Store the result with list() if you need it more than once, or create a new zip() each time.
Losing data to unequal lengths
✗ Wrong
names = ["Asha", "Ravi", "Meera"]
marks = [88, 92]
print(list(zip(names, marks)))✓ Right
names = ["Asha", "Ravi", "Meera"]
marks = [88, 92]
try:
print(list(zip(names, marks, strict=True)))
except ValueError:
print("length mismatch")Use strict=True to catch the mismatch, or zip_longest to keep every name.
Indexing a zip object
✗ Wrong
pairs = zip([1, 2], [3, 4])
print(pairs[0])✓ Right
pairs = list(zip([1, 2], [3, 4]))
print(pairs[0])Convert to a list before indexing.
Python zip() Function: questions people ask
What does zip() do in Python?
It pairs up items from two or more iterables by position and returns an iterator of tuples. The first tuple holds every first item, the second holds every second item, and so on.
What happens if the lists passed to zip() have different lengths?
By default zip() stops at the shortest list and ignores the rest. Use strict=True to raise a ValueError instead.
How do I unzip a list of tuples in Python?
Use zip(*pairs). The star unpacks the list of tuples so that zip() regroups them into one tuple per position.
Keep learning
Step-by-step lessons:
Python enumerate() and zip()Python for LoopsPython Dictionaries
Related:
Python enumerate() FunctionPython map() FunctionPython sorted() FunctionPython itertools
More in built-in functions:
Python print() FunctionPython len() FunctionPython range() FunctionPython type() FunctionPython int() FunctionPython float() FunctionPython str() FunctionPython input() Function See all →