Many coders struggle with understanding the zip function. Let’s fix this!
zip() function takes an arbitrary number of
[1, 2, 3] and
[4, 5, 6] to
[(1,4), (2,5), (3,6)].
How to zip two lists in Python?
Say, you have two lists:
Now you zip them together and get the new list:
[(1,4), (2,5), (3,6)]
How to unzip a zipped list in Python?
Let’s start with this zipped list:
[(1,4), (2,5), (3,6)]
You can unzip the list by using the following trick: If you remove the outer bracket of the result (e.g. via the asterisk operator), you get the following three tuples:
(1,4) (2,5) (3,6)
When you zip those together, you get the new list:
[(1, 2, 3), (4, 5, 6)]
So you (almost) have your two original lists again!
This is the idea in the following code snippet to zip two lists and unzip the resulting list into two lists again:
lst_1 = [1, 2, 3] lst_2 = [4, 5, 6] # Zip two lists together zipped = list(zip(lst_1, lst_2)) print(zipped) # [(1, 4), (2, 5), (3, 6)] # Unzip to lists again lst_1_new, lst_2_new = zip(*zipped) print(list(lst_1_new)) print(list(lst_2_new))
Puzzle: what will be the output of the last two print statements?
zip() function takes a number of iterables and aggregates them to a single one by combining the i-th values of each iterable into a tuple. You can unzip this list of tuples by calling
zip(*list) using the unpacking (asterisk) operator
The rest of this article is about answering your questions regarding the
How to zip lists of different lengths?
It simply ignores the remaining elements of the larger list. Here is an example:
print(list(zip([1,2,3],[1,2]))) # [(1, 1), (2, 2)]
How to use zip with a single argument?
You can do that. But the more interesting question is: what will happen?
Python still creates a tuple of the
print(list(zip([1,2,3]))) # [(1,), (2,), (3,)]
This strange output formatting caused some confusion among the readers of my Python email course (join us, it’s free!). I hope that you now understand completely that this is not a bug in Python but only a tuple with a single element. (Don’t ask me why they didn’t use the format ‘(x)’ instead of ‘(x,)’.)
What is a zip object in Python?
You will quickly realize that the result of the zip function is neither a list nor a tuple:
x = [[1,2],[3,4]] print(zip(*x))
You would expect [(1,3),(2,4)] but the result is “<zip object at 0x000002E9D87CFD08>”. That’s weird, isn’t it
Well — actually not. The result of the zip() function is an iterator, not a list.
An iterator in Python is an object that contains a fixed number of
To fix this, you have to convert the iterator object in the iterable you want (e.g. set, list, tuple):
x = [[1,2],[3,4]] print(list(zip(*x))) # [(1, 3), (2, 4)]
Finally, let me clarify one last thing: the asterisk operator is placed just before the iterable to be unpacked (not after it or anywhere else). If you put the asterisk operator anywhere else, Python will think it’s multiplication and throw an error (best case):
x = [[1,2],[3,4]] y = zip*(x) # NO! y = zip(x*) # NO! y = *zip(x) # No! (It's an iterator not an iterable) y = zip(*x) # Yes!
Where to go from here?
This article is based on one of my free Python Email lectures. I call it “Coffee Break Python Email Course” because it will improve your Python skills in your coffee break. You just have to read my daily emails and participate in the fun coding challenges. My >5,000 readers love it.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.