Unzip a List of Tuples in Python

Introduction:

In this tutorial we are learning about unzip a list of tuples in Python. Python is a well-known programming language that is used worldwide for many purposes, such as machine learning, web development, and data science, and enables many different processes. Tuples are a useful Python feature that helps store data from multiple datasets (dictionaries, lists, etc.) at a single level. The zipping technique is assigning a key value or pairs from two different lists, has been covered in many articles before, and sometimes we have special equipment to work on it. This work can be done in several ways. In this tutorial, we look at different methods you can use to unzip a list of tuples in Python.

Different ways for unzipping a list of tuples in Python:

Here, we discuss different ways to unzip a list of tuples in Python, which are given below -

1. Using the list comprehension in Python:

List comprehensions are used to identify each item in a list individually. They are the easiest way to do unzipping tasks. In this way, we will present the list of tuples with the help of list comprehension. It is only sometimes used for this job, but it is a good way to start.

Program Code 1:

Here, we are given the program code for unzipping a list of tuples using the list comprehension in Python. The code is given below -

Output:

Now, we run the above code and find the list after unzipping it. The result is given below -

The given list is: [('Priya', 1), ('is', 2), ('a', 3), ('good', 4), ('programmer', 5)]
The unzipping list is: [['Priya', 'is', 'a', 'good', 'programmer'], [1, 2, 3, 4, 5]]

Program Code 2:

Here, we are given another program code for unzipping a list of tuples using the list comprehension in Python. The code is given below -

Output:

Now, we run the above code and find the list after unzipping it. The result is given below -

The given list is: [('Rose', 'Flower'), ('Orange', 'Fruit'), ('Crow', 'Bird'), ('Green', 'Colour')]
The unzipping list is: 
('Rose', 'Orange', 'Crow', 'Green')
('Flower', 'Fruit', 'Bird', 'Colour')

2. Using the zip() function and the * operator in Python:

The most common unzip method, most common Pythonic, and recommended method. Programmers around the world often use this method to do this job. The * operator unzips tuples into separate lists.

Program Code:

Here, we are given the program code for unzipping a list of tuples using the zip() function and the * operator in Python. The code is given below -

Output:

Now, we run the above code and find the list after unzipping it. The result is given below -

The given list is: [('Priya', 1), ('is', 2), ('a', 3), ('good', 4), ('programmer', 5)]
The unzipping list is: [('Priya', 'is', 'a', 'good', 'programmer'), (1, 2, 3, 4, 5)]

3. Using the map() function in Python:

This is another less well-known way to do unzipping work. It is one way to accomplish this task. This also uses the * operator to perform a simple opening of the list of tuples. The map() function is deprecated in Python version >= 3.

Program Code:

Here, we are given the program code for unzipping a list of tuples using the map() function in Python. The code is given below -

Output:

Now, we run the above code and find the list after unzipping it. The result is given below -

The given list is: [('Priya', 1), ('is', 2), ('a', 3), ('good', 4), ('programmer', 5)]
The unzipping list is: <map object at 0x000001BCD0585FD0>

4. Using the lists and the tuple() method in Python:

Another way to unzip a list of tuples in Python is to use lists and the tuple() method.

Program Code:

Here, we are given the program code for unzipping a list of tuples using the lists and the tuple() method in Python. The code is given below -

Output:

Now, we run the above code and find the list after unzipping it. The result is given below -

The given list is: [('Priya', 1), ('is', 2), ('a', 3), ('good', 4), ('programmer', 5)]
The unzipping list is: [('Priya', 'is', 'a', 'good', 'programmer'), (1, 2, 3, 4, 5)]

5. Using the NumPy in Python:

Using NumPy is another way to unzip a list of tuples in Python. This method is important when the list of tuples contains different data. In this case, we can easily use numpy's functionality.

Program Code:

Here, we are given the program code for unzipping a list of tuples using NumPy in Python. The code is given below -

Output:

Now, we run the above code and find the list after unzipping it. The result is given below -

The given list is: [('Rose', 'Flower'), ('Orange', 'Fruit'), ('Crow', 'Bird'), ('Green', 'Colour')]
The unzipping list is: 
['Rose', 'Orange', 'Crow', 'Green']
['Flower', 'Fruit', 'Bird', 'Colour']

6. Using the loop method in Python:

Unzipping a list of tuples in Python using the loop method is one of the simplest methods. This method is used when no variable is defined in Python.

Program Code:

Here, we are given the program code for unzipping a list of tuples using the loop method in Python. The code is given below -

Output:

Now, we run the above code and find the list after unzipping it. The result is given below -

The given list is: [('Rose', 'Flower'), ('Orange', 'Fruit'), ('Crow', 'Bird'), ('Green', 'Colour')]
The unzipping list is: 
['Rose', 'Orange', 'Crow', 'Green']
['Flower', 'Fruit', 'Bird', 'Colour']

7. Using the Panda Data Frame in Python:

Unzipping the list of tuples in Python using the Panda Data Frame is an excellent method, but it is only used when large files need to be highly accurate.

Program Code:

Here, we are given the program code for unzipping a list of tuples using the Panda Data Frame in Python. The code is given below -

Output:

Now, we run the above code and find the list after unzipping it. The result is given below -

The given list is: [('Rose', 'Flower'), ('Orange', 'Fruit'), ('Crow', 'Bird'), ('Green', 'Colour')]
The unzipping list is: 
['Rose', 'Orange', 'Crow', 'Green']
['Flower', 'Fruit', 'Bird', 'Colour']

8. Using the operator module in Python:

Unzipping a list of tuples in Python is easy if you use the operator module's functionality. However, this method is also very difficult and less effective.

Program Code:

Here, we are given the program code for unzipping a list of tuples using the operator module in Python. The code is given below -

Output:

Now, we run the above code and find the list after unzipping it. The result is given below -

The given list is: [('Rose', 'Flower'), ('Orange', 'Fruit'), ('Crow', 'Bird'), ('Green', 'Colour')]
The unzipping list is: 
['Flower', 'Fruit', 'Bird', 'Colour']

9. Using the re method in Python:

The code extracts strings and integers from lists of arrays using regular expressions. It then creates a modified list that contains two sublists. The first one contains the string values, and the other contains the integer values. The step of its is given in below -

  1. Firstly, we import the re-module using the regular expression.
  2. Then, we define the list of tuples l.
  3. After that, convert the list of tuples l into the string str_l.
  4. Using the re.findall() function to extract all string values in str_l, we use the regular expression r" '(.*?)'". This regular expression matches any combination of two single quotes (including quotes) and returns the matching string as a group.
  5. Then, use the re.findall() function again to extract all integers in str_l using the regular expression \d+. This regular expression matches a string of one or more digits.
  6. After that, use the list() function and the map() function to convert the equation obtained in step 5 to actual numbers and create a modified list by combining the two lists obtained in steps 4 and step 6.
  7. Lastly, we print the original list and the unzipping list.

These steps are followed when we write a program in Python for unzipping a list of tuples using the re method.

Program Code:

Here, we are given the program code for unzipping a list of tuples using the re method in Python. We are following the above steps to write this code. The code is given below -

Output:

Now, we run the above code and find the list after unzipping it. The result is given below -

The given list is: [('Priya', 1), ('is', 2), ('a', 3), ('good', 4), ('programmer', 5)]
The unzipping list is: [['Priya', 'is', 'a', 'good', 'programmer'], [1, 2, 3, 4, 5]]

Conclusion:

In this tutorial we are learning about unzip a list of tuples in Python. To be a good programmer, you need to understand the various methods that can be used to unzip the list of tuples. Different methods can be used depending on convenience and application area. Here we learn various kind of methods for unzipping the list of tuples along with the examples.