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:
We can apply zip on these two lists:
This will give an output like the following:
which is not quite comprehensible. To make sense of it, you should convert the zip object into a list before printing:
The output is:
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:
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:

Method 2: Unzipping a list using a while loop

A second approach is to replace the for loop with a while loop.
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:

Method 3: Unzipping a list using list comprehension

We can accomplish our unzipping goals in a succinct manner using list comprehensions:
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:

Method 4: Unzipping a list using the zip() function and the * operator

This method is the preferred, Python-ic, way to unzip a list.
The output is as before but with a slight change in format:
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.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2024. All rights reserved.