Zip & Unzip: How Does It Work in Python?

The 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 for every i.

(An iter-able is an object that contains multiple elements over which you can iterate. Examples are lists, sets, or tuples.)

Say, you have two lists:


Now you zip them together and get the new list:

[(1,4), (2,5), (3,6)]

You can unzip them 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:


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:

lst_1 = [1, 2, 3]
lst_2 = [4, 5, 6]

# Zip two lists together
zipped = list(zip(lst_1, lst_2))
# [(1, 4), (2, 5), (3, 6)]

# Unzip to lists again
lst_1_new, lst_2_new = zip(*zipped)

Puzzle: what will be the output of the last two print statements?

The rest of the article is about answering any question you may have regarding the zip() function.

What if you zip lists of different lengths?

It simply ignores the remaining elements of the longer list. Here is an example:

# [(1, 1), (2, 2)]

Can we use zip with a single argument?

Maybe you remember that I did this a few emails ago to trick you. So: Yes, you can do that. Python still creates a tuple of the i-th elements — only that there is only one of those. Thus, the tuple has only a single element. The default output of a tuple with one element x is (x,).

# [(1,), (2,), (3,)]

This strange output formatting caused some confusion. Many students of my free email course stumbled upon it (join us if you want to continuously improve in Python — day after day).

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]]

You would expect [(1,3),(2,4)] but the result is “<zip object at 0x000002E9D87CFD08>”. That’s weird, isn’t it?

Actually not. The result of the zip() function is an iterator. An iterator in Python is an object that contains a fixed number of elements and allows you to access each element in an ordered fashion (the next(iterator) function for an iterator). This is more efficient and more general-purpose — compared to creating a list and returning the list as a result.

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]]
# [(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!

Leave a Comment

Your email address will not be published. Required fields are marked *