Python zip() Function Explained
zip() is a built-in Python function that combines items from two or more iterables by position.
It is useful when you want to work with related values at the same time, such as names and scores, or keys and values. Beginners often use zip() inside a for loop or convert its result into a list or dictionary.
Quick example
names = ["Ana", "Ben", "Cara"]
scores = [90, 85, 88]
for name, score in zip(names, scores):
print(name, score)
Output:
Ana 90
Ben 85
Cara 88
Use zip() when you want to loop through two or more iterables at the same time. It pairs items by position.
What zip() does
zip()combines items from two or more iterables- It groups items by matching positions
- The result is a zip object, which is an iterator
- A zip object can be looped over or converted to a list
For example, if you combine two lists, the first item from each list becomes one pair, the second item from each list becomes another pair, and so on.
Basic syntax
The basic syntax is:
zip(iterable1, iterable2, ...)
Each step creates one tuple of matched items.
If you pass two iterables, each result item has two values:
numbers = [1, 2, 3]
letters = ["a", "b", "c"]
print(list(zip(numbers, letters)))
Output:
[(1, 'a'), (2, 'b'), (3, 'c')]
If you pass three iterables, each result item has three values:
names = ["Ana", "Ben", "Cara"]
scores = [90, 85, 88]
grades = ["A", "B", "B+"]
print(list(zip(names, scores, grades)))
Output:
[('Ana', 90, 'A'), ('Ben', 85, 'B'), ('Cara', 88, 'B+')]
Simple example with two lists
A common beginner use is looping through two related lists at the same time.
names = ["Ana", "Ben", "Cara"]
scores = [90, 85, 88]
for name, score in zip(names, scores):
print(f"{name} scored {score}")
Output:
Ana scored 90
Ben scored 85
Cara scored 88
Here is what happens:
zip(names, scores)creates pairs like("Ana", 90)- The loop runs once for each pair
nameandscoreunpack the tuple into separate variables
If tuple unpacking feels unfamiliar, it may help to first practice how to loop through a list in Python.
What happens when lengths are different
zip() stops at the shortest iterable.
That means extra items in longer iterables are ignored.
names = ["Ana", "Ben", "Cara", "Dan"]
scores = [90, 85]
for name, score in zip(names, scores):
print(name, score)
Output:
Ana 90
Ben 85
Notice that "Cara" and "Dan" are not used.
This behavior is often helpful, but it can also hide mistakes. If you expected all items to be paired, check the lengths first:
names = ["Ana", "Ben", "Cara", "Dan"]
scores = [90, 85]
print(len(names), len(scores))
Output:
4 2
If missing pairs are unexpected, compare the lengths before using zip().
Converting zip() output
Because zip() returns a zip object, you often need to convert it.
Use list(zip(...))
This is the easiest way to see all pairs at once.
names = ["Ana", "Ben", "Cara"]
scores = [90, 85, 88]
pairs = list(zip(names, scores))
print(pairs)
Output:
[('Ana', 90), ('Ben', 85), ('Cara', 88)]
Use dict(zip(keys, values))
This is a common way to build a dictionary.
keys = ["name", "age", "city"]
values = ["Ana", 25, "Lima"]
person = dict(zip(keys, values))
print(person)
Output:
{'name': 'Ana', 'age': 25, 'city': 'Lima'}
If you want more dictionary examples, see how to merge dictionaries in Python.
A zip object is consumed after use
A zip object is an iterator. After you loop over it once, it is exhausted.
names = ["Ana", "Ben"]
scores = [90, 85]
pairs = zip(names, scores)
print(list(pairs))
print(list(pairs))
Output:
[('Ana', 90), ('Ben', 85)]
[]
If you need the data again, convert it to a list first or create a new zip object.
For a deeper explanation, see iterators and iterable objects explained.
Common beginner use cases
Here are some simple ways beginners use zip().
Loop through names and scores together
names = ["Ana", "Ben", "Cara"]
scores = [90, 85, 88]
for name, score in zip(names, scores):
print(f"{name}: {score}")
Combine keys and values into a dictionary
keys = ["id", "name", "active"]
values = [101, "Ana", True]
data = dict(zip(keys, values))
print(data)
Compare two lists item by item
list1 = [10, 20, 30]
list2 = [10, 25, 30]
for a, b in zip(list1, list2):
print(a == b)
Output:
True
False
True
Print related values in the same loop
products = ["Book", "Pen", "Bag"]
prices = [12.5, 1.2, 25.0]
for product, price in zip(products, prices):
print(f"{product} costs ${price}")
If you want more practical examples, see how to use zip in Python.
Common mistakes with zip()
Expecting zip() to keep going to the longest iterable
zip() does not do that. It stops at the shortest iterable.
If values seem to be missing, compare the lengths:
print(len(list1), len(list2))
Trying to reuse the same zip object after looping once
This is a very common mistake.
pairs = zip([1, 2], ["a", "b"])
for item in pairs:
print(item)
for item in pairs:
print(item)
The second loop prints nothing because the zip object has already been used.
Forgetting to unpack tuple values in a loop
This is correct:
names = ["Ana", "Ben"]
scores = [90, 85]
for name, score in zip(names, scores):
print(name, score)
If you use only one variable, you get the whole tuple:
for item in zip(names, scores):
print(item)
Output:
('Ana', 90)
('Ben', 85)
Both forms are valid, but they do different things.
Using zip() when you actually need indexes with enumerate()
Use zip() when you want values from multiple iterables.
Use enumerate() when you need a counter or index.
names = ["Ana", "Ben", "Cara"]
for index, name in enumerate(names):
print(index, name)
If that pattern is what you need, read Python enumerate() function explained.
FAQ
What does zip() return in Python?
It returns a zip object, which is an iterator that produces tuples of matched items.
Does zip() work with different lengths?
Yes, but it stops when the shortest iterable runs out of items.
How do I see the values from zip()?
Loop over it or convert it with list(zip(...)).
Can I make a dictionary with zip()?
Yes. Use dict(zip(keys, values)) when one iterable has keys and the other has values.
Why is my zip object empty the second time?
A zip object is an iterator. After it has been used once, it is exhausted.