Kodeclik Blog
How to unzip a list of tuples in Python
Recall that the zip function takes iterables as input and pairs the first items in the iterables together, then the second items in the iterables together, and so on.
For instance, if we have a list of names like so:
first_names = ['Mickey','Donald','Harry']
last_names = ['Mouse','Duck','Potter']
We can apply zip on these two lists:
print(zip(first_names,last_names))
This will give an output like the following:
<zip object at 0x7f9e8006ecc0>
which is not quite comprehensible. To make sense of it, you should convert the zip object into a list before printing:
print(list(zip(first_names,last_names)))
The output is:
[('Mickey', 'Mouse'), ('Donald', 'Duck'), ('Harry', 'Potter')]
You can see that the output is a list of paired tuples. In this blog we will see how to take a paired output from a zip function and unzip them (to yield the original lists that we had).
There are at least four different ways to implement an unzipping feature.
Method 1: Unzipping a list using a for loop
The easiest way to unzip a list of paired tuples is to use a simple for loop. Here is how that might work:
first_names = ['Mickey','Donald','Harry']
last_names = ['Mouse','Duck','Potter']
paired_names = list(zip(first_names,last_names))
f = []; l = []
for x in range(len(paired_names)):
(first,last) = paired_names[x]
f.append(first)
l.append(last)
print(f)
print(l)
As you can see here, paired_names contains the zipped list that we are trying to unzip. We setup a for loop that goes in length as much as the length of “paired_names”. We cycle through each element of “paired_names” in turn and for each element (which is a tuple) we extract the first and last names and append them to separate lists. Finally, after the loop is done, we print these two lists.
The output is:
['Mickey', 'Donald', 'Harry']
['Mouse', 'Duck', 'Potter']
Method 2: Unzipping a list using a while loop
A second approach is to replace the for loop with a while loop.
first_names = ['Mickey','Donald','Harry']
last_names = ['Mouse','Duck','Potter']
paired_names = list(zip(first_names,last_names))
f = []; l = []
x = 0
while x < len(paired_names):
(first,last) = paired_names[x]
f.append(first)
l.append(last)
x = x+1
print(f)
print(l)
Note that in this program we initialize the variable “x” to zero, then increment it step by step inside the while loop till we reach the end of the list of tuples (i.e., the length of the “paired_names” list). Inside the while loop, the actual operation is still the same.
The output is still:
['Mickey', 'Donald', 'Harry']
['Mouse', 'Duck', 'Potter']
Method 3: Unzipping a list using list comprehension
We can accomplish our unzipping goals in a succinct manner using list comprehensions:
first_names = ['Mickey','Donald','Harry']
last_names = ['Mouse','Duck','Potter']
paired_names = list(zip(first_names,last_names))
f = [i for (i, j) in paired_names]
l = [j for (i, j) in paired_names]
print(f)
print(l)
Note that we have a single line of code that computes the “f” list. In that line, we are iterating over the length of the “paired_names” list and using a list comprehension to add the necessary elements instead of using an append function.
The output is as before:
['Mickey', 'Donald', 'Harry']
['Mouse', 'Duck', 'Potter']
Method 4: Unzipping a list using the zip() function and the * operator
This method is the preferred, Python-ic, way to unzip a list.
first_names = ['Mickey','Donald','Harry']
last_names = ['Mouse','Duck','Potter']
paired_names = list(zip(first_names,last_names))
unzipped = list(zip(*paired_names))
print(unzipped)
The output is as before but with a slight change in format:
[('Mickey', 'Donald', 'Harry'), ('Mouse', 'Duck', 'Potter')]
In other words, the unzipped versions are in the same list. Now why does this method work? The “*” operator by itself unzips a list into independent lists. Then we zip them into a list (so that the unzipped values occur one after the other). Then we convert that zipped object into a list.
The advantage of this method is that it will work for any size tuples, not just tuples having two elements (as we have here).
We have learnt four different ways to unzip a list of tuples in Python. Which one is your favorite?
The above codes have not been rigorously tested for all cases. For instance, if one of the tuples has only one element, you will need to do some error handling. We will leave it as an exercise for you to handle these cases.
For more Python content, checkout the math.ceil() and math.floor() functions! Also
learn about the math domain error in Python and how to fix it!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.