Last modified: Nov 19, 2026
Python zip Function: Combine Iterables
The Python zip function is one of the handiest built-ins for working with multiple sequences. It pairs items from two or more iterables into tuples. This lets you loop over several lists at once.
This guide explains how zip works, shows practical examples, and covers common pitfalls. By the end, you will use it with confidence.
What Is the Python zip Function?
zip takes two or more iterables as arguments. It returns an iterator of tuples. Each tuple holds one item from every iterable.
The pairing stops at the shortest iterable. Extra items in longer sequences are ignored by default.
names = ["Alice", "Bob", "Carol"]
scores = [85, 92, 78]
paired = zip(names, scores)
print(list(paired))
[('Alice', 85), ('Bob', 92), ('Carol', 78)]
The result is a list of tuples. The original lists are unchanged. zip does not modify its inputs.
How zip Works Step by Step
Think of a zipper on a jacket. It joins two sides tooth by tooth. zip joins iterables item by item in the same way.
In Python 3, zip returns a lazy iterator. It produces tuples only when you ask for them. This saves memory on large data.
letters = ["a", "b", "c"]
numbers = [1, 2, 3]
zipped = zip(letters, numbers)
print(zipped) # iterator object
print(next(zipped)) # first tuple
print(next(zipped)) # second tuple
('a', 1)
('b', 2)
You can wrap the result in list or tuple to see everything at once. This is useful for quick checks.
Looping Over Multiple Lists with zip
The most common use is looping over several lists in one for loop. You unpack each tuple directly into variables.
products = ["pen", "notebook", "eraser"]
prices = [1.5, 3.0, 0.75]
for product, price in zip(products, prices):
print(f"{product} costs ${price}")
pen costs $1.5
notebook costs $3.0
eraser costs $0.75
This pattern is clean and readable. It avoids manual index tracking. If you like index-based loops, the Python enumerate complete guide shows a good alternative for single lists.
Zipping More Than Two Iterables
zip accepts any number of iterables. Each tuple will contain one item from each one.
names = ["Alice", "Bob"]
ages = [30, 25]
cities = ["Paris", "Tokyo"]
for name, age, city in zip(names, ages, cities):
print(name, age, city)
Alice 30 Paris
Bob 25 Tokyo
Keep the order consistent. The first tuple holds the first item of each iterable, and so on.
What Happens with Unequal Lengths?
When the iterables differ in length, zip stops at the shortest one. Extra elements are silently dropped.
a = [1, 2, 3, 4]
b = ["x", "y"]
print(list(zip(a, b)))
[(1, 'x'), (2, 'y')]
This behavior is often useful. Sometimes it hides bugs. If you need all items, use itertools.zip_longest.
from itertools import zip_longest
a = [1, 2, 3, 4]
b = ["x", "y"]
print(list(zip_longest(a, b, fillvalue="?")))
[(1, 'x'), (2, 'y'), (3, '?'), (4, '?')]
The fillvalue argument fills missing slots. This keeps every item from the longest iterable.
Unzipping with zip
You can reverse the process. Use the unpacking operator * to split tuples back into separate sequences.
pairs = [("Alice", 85), ("Bob", 92), ("Carol", 78)]
names, scores = zip(*pairs)
print(names)
print(scores)
('Alice', 'Bob', 'Carol')
(85, 92, 78)
This trick is called unzipping. It is handy when you need to transpose data.
Creating Dictionaries with zip
zip pairs keys with values. Pass the result to dict to build a dictionary in one line.
keys = ["name", "age", "city"]
values = ["Alice", 30, "Paris"]
person = dict(zip(keys, values))
print(person)
{'name': 'Alice', 'age': 30, 'city': 'Paris'}
This is a common pattern for turning two lists into a mapping. It is short and easy to read.
Using zip with Other Built-ins
zip works well with functions like sum, max, and min. You can compute values across paired data.
prices = [10, 20, 30]
quantities = [2, 1, 4]
totals = [p * q for p, q in zip(prices, quantities)]
print(totals)
print(sum(totals))
[20, 20, 120]
160
If you also need to find extremes in a list, the guide on how to find max and min in a list pairs nicely with these techniques.
Common Mistakes to Avoid
Mistake one: reusing a zip iterator. Once consumed, it is empty.
z = zip([1, 2], ["a", "b"])
print(list(z))
print(list(z)) # empty on second use
[(1, 'a'), (2, 'b')]
[]
Recreate the zip object if you need it again. Or convert it to a list first.
Mistake two: forgetting that zip stops early. Always check lengths when data matters.
Mistake three: assuming zip sorts data. It does not. It pairs items in their existing order.
zip vs. Manual Indexing
Beginners often loop with indexes. zip is cleaner and less error-prone.
# Manual indexing
a = [1, 2, 3]
b = ["x", "y", "z"]
for i in range(len(a)):
print(a[i], b[i])
# With zip
for x, y in zip(a, b):
print(x, y)
1 x
2 y
3 z
1 x
2 y
3 z
The zip version is shorter and safer. It also works with any iterable, not just lists.
Real-World Example: Combining Data
Imagine you have separate lists of student names and grades. You want a simple report.
students = ["Alice", "Bob", "Carol"]
grades = [88, 74, 95]
report = dict(zip(students, grades))
for name, grade in report.items():
status = "Pass" if grade >= 80 else "Review"
print(f"{name}: {grade} ({status})")
Alice: 88 (Pass)
Bob: 74 (Review)
Carol: 95 (Pass)
This example combines zip, a dictionary, and a conditional expression. It shows how small pieces work together.
When Not to Use zip
If your iterables have very different lengths, zip may drop data. Use zip_longest instead.
If you need index positions too, loop with enumerate over a zip result. That gives you both the counter and the paired values.
names = ["Alice", "Bob"]
scores = [85, 92]
for index, (name, score) in enumerate(zip(names, scores), start=1):
print(index, name, score)
1 Alice 85
2 Bob 92
This nesting is readable when you unpack carefully. Practice it a few times and it becomes natural.
Performance Notes
zip is implemented in C, so it is fast. It creates tuples lazily, which keeps memory use low.
For huge datasets, avoid converting to a list unless you truly need all pairs at once. Iterate directly instead.
Also remember that zip is single-use. Plan your code around that fact.
Related Python Basics to Learn Next
Once you master zip, try small projects. Building a Python simple calculator program is a great way to practice loops and input handling.
You can also explore text processing with regex. Learning how to compile a regex pattern helps you write faster and more reusable pattern code.
Conclusion
The Python zip function is a small tool with big value. It pairs items from multiple iterables into tuples. It stops at the shortest sequence. It returns a lazy iterator in Python 3.
Use zip to loop over parallel lists, build dictionaries, and unzip data. Watch out for unequal lengths and single-use iterators.
With a few examples under your belt, zip becomes second nature. It makes your code shorter, cleaner, and easier to read. Start using it in your next script and see the difference.