How to Use zip() in Python
zip() lets you combine items from two or more iterables by their position.
It is useful when you want to:
- loop through two lists at the same time
- create pairs of related values
- turn two lists into a dictionary
For beginners, the most common use is looping through related data like names and scores.
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 stops when the shortest iterable ends.
What zip() does
zip() combines items from multiple iterables by position.
For example, if you have:
names = ["Ana", "Ben", "Cara"]
scores = [90, 85, 88]
Then:
print(list(zip(names, scores)))
Output:
[('Ana', 90), ('Ben', 85), ('Cara', 88)]
Each grouped result contains:
- the first item from each iterable
- then the second item from each iterable
- then the third item from each iterable
So zip(names, scores) creates pairs like:
("Ana", 90)("Ben", 85)("Cara", 88)
If you are new to loops, see how to loop through a list in Python.
Use zip() in a for loop
This is the most common way to use zip().
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
How this works
zip(names, scores)groups matching items together- each grouped result has two values
for name, score in ...unpacks those two values into separate variables
This is useful when two lists belong together by position.
For example:
- the first name matches the first score
- the second name matches the second score
If you forget to unpack, you can still loop like this:
names = ["Ana", "Ben"]
scores = [90, 85]
for item in zip(names, scores):
print(item)
Output:
('Ana', 90)
('Ben', 85)
See the result of zip()
A common beginner confusion is that zip() does not return a normal list.
It returns a zip object.
result = zip([1, 2, 3], ["a", "b", "c"])
print(result)
print(type(result))
Output:
<zip object at 0x...>
<class 'zip'>
If you want to see all grouped items, convert it to a list:
result = zip([1, 2, 3], ["a", "b", "c"])
print(list(result))
Output:
[(1, 'a'), (2, 'b'), (3, 'c')]
This is especially helpful when debugging.
You can also test quickly with:
print(list(zip([1, 2, 3], ['a', 'b', 'c'])))
Output:
[(1, 'a'), (2, 'b'), (3, 'c')]
To learn more about what a zip object is, see Python zip() function explained and iterators and iterable objects explained.
Use zip() with more than two iterables
zip() can combine three or more iterables, not just two.
names = ["Ana", "Ben", "Cara"]
scores = [90, 85, 88]
grades = ["A", "B", "B+"]
for name, score, grade in zip(names, scores, grades):
print(name, score, grade)
Output:
Ana 90 A
Ben 85 B
Cara 88 B+
Each result now contains one item from each iterable.
You can also view the grouped values directly:
print(list(zip(names, scores, grades)))
Output:
[('Ana', 90, 'A'), ('Ben', 85, 'B'), ('Cara', 88, 'B+')]
What happens if lengths are different
zip() stops when the shortest iterable runs out.
This is very important.
numbers = [1, 2, 3]
letters = ["a", "b"]
print(list(zip(numbers, letters)))
Output:
[(1, 'a'), (2, 'b')]
The value 3 is ignored because letters has no third item.
Why this matters
Many beginners expect zip() to keep all items from the longest list. It does not.
So if your data has different lengths:
- extra items in longer iterables are ignored
- you may lose data without noticing
A quick debug check is:
print(list(zip([1, 2, 3], ['a', 'b'])))
Output:
[(1, 'a'), (2, 'b')]
If you need every item from both sides, zip() may not be the right tool.
Create a dictionary from two lists
A very practical use of zip() is building a dictionary from keys and values.
keys = ["name", "age", "city"]
values = ["Ana", 20, "Lima"]
person = dict(zip(keys, values))
print(person)
Output:
{'name': 'Ana', 'age': 20, 'city': 'Lima'}
This works because:
- the first key matches the first value
- the second key matches the second value
- and so on
This is a simple way to turn paired data into a dictionary.
If you want more dictionary examples, see how to merge dictionaries in Python.
Unzip paired data
You can also split paired data back into separate variables.
pairs = [("Ana", 90), ("Ben", 85), ("Cara", 88)]
names, scores = zip(*pairs)
print(names)
print(scores)
Output:
('Ana', 'Ben', 'Cara')
(90, 85, 88)
What * means here
The * unpacks the list of pairs.
Without *, zip() would treat pairs as one iterable of tuples.
With *pairs, each tuple is passed in separately, so zip() groups the first values together and the second values together.
So:
("Ana", 90)("Ben", 85)("Cara", 88)
becomes:
("Ana", "Ben", "Cara")(90, 85, 88)
When zip() is a good choice
Use zip() when:
- items belong together by position
- you want to loop through two or more iterables side by side
- you want to create pairs or grouped values
- you want to build a dictionary from matching keys and values
Do not use zip() when:
- you need to keep extra items from longer iterables
- your iterables are not in the correct matching order
- the values do not belong together by position
In some loops, you may also want the position number along with the values. In that case, how to use enumerate() in Python is often the next tool to learn.
Common mistakes
Here are some common problems beginners run into with zip().
Printing zip(...) directly
This:
print(zip([1, 2], ["a", "b"]))
prints a zip object, not the pairs.
Use:
print(list(zip([1, 2], ["a", "b"])))
Assuming zip() keeps all items
This:
print(list(zip([1, 2, 3], ["a", "b"])))
does not include 3.
zip() stops at the shortest iterable.
Forgetting to unpack in the loop
This works:
names = ["Ana", "Ben"]
scores = [90, 85]
for name, score in zip(names, scores):
print(name, score)
If you write:
for name in zip(names, scores):
print(name)
then name actually holds the whole tuple, such as ("Ana", 90).
Using data in the wrong order
zip() matches by position only.
If one list is out of order, the pairing will also be wrong.
names = ["Ana", "Ben"]
scores = [85, 90]
print(list(zip(names, scores)))
Output:
[('Ana', 85), ('Ben', 90)]
Python assumes those positions match.
Trying to index a zip object
A zip object is not a list.
This will not work:
result = zip([1, 2], ["a", "b"])
# print(result[0])
If you need indexing, convert it first:
result = list(zip([1, 2], ["a", "b"]))
print(result[0])
Output:
(1, 'a')
FAQ
What does zip() return in Python?
It returns a zip object that produces grouped items one by one. Convert it to a list if you want to see all results at once.
Does zip() work with lists only?
No. It works with many iterable types, such as lists, tuples, strings, and ranges.
What happens if one list is longer than the other?
zip() stops at the shortest iterable, so extra items in the longer one are ignored.
How do I turn zip() output into a dictionary?
Use dict(zip(keys, values)) when one list has keys and the other has matching values.
How do I see the pairs created by zip()?
Wrap it in list(), like list(zip(a, b)).