Python | Unzip a list of tuples
The zipping techniques, that is assigning a key-value or pairing from two different lists has been covered in many articles before, sometimes we have specific utility to perform the reverse task. This task can be achieved by various methods.
Let’s discuss some of the methods to unzip a list of tuples.
Method #1 : Using List Comprehension
Using list comprehension is the most naive approach to perform this task of unzipping and usually not used to perform this task, but good method to start with.
# Python3 code to demonstrate # Unzip a list of tuples # using list comprehension # initializing list of tuples test_list = [('Akshat', 1), ('Bro', 2), ('is', 3), ('Placed', 4)] # Printing original list print ("Original list is : " + str(test_list)) # using list comprehension to # perform Unzipping res = [[ i for i, j in test_list ], [ j for i, j in test_list ]] # Printing modified list print ("Modified list is : " + str(res)) |
Output :
Original list is : [('Akshat', 1), ('Bro', 2), ('is', 3), ('Placed', 4)]
Modified list is : [['Akshat', 'Bro', 'is', 'Placed'], [1, 2, 3, 4]]
Method #2 : Using zip() and * operator
Mostly used method to perform unzip and most Pythonic and recommended as well. This method is generally used to perform this task by programmers all over. * operator unzips the tuples into independent lists.
# Python3 code to demonstrate # Unzip a list of tuples # using zip() and * operator # initializing list of tuples test_list = [('Akshat', 1), ('Bro', 2), ('is', 3), ('Placed', 4)] # Printing original list print ("Original list is : " + str(test_list)) # using zip() and * operator to # perform Unzipping res = list(zip(*test_list)) # Printing modified list print ("Modified list is : " + str(res)) |
Output :
Original list is : [('Akshat', 1), ('Bro', 2), ('is', 3), ('Placed', 4)]
Modified list is : [('Akshat', 'Bro', 'is', 'Placed'), (1, 2, 3, 4)]
Method #3 : Using map()
This is yet another way that can be employed to perform this task of unzipping which is less known but indeed a method to achieve this task. This also uses the * operator to perform the basic unpacking of the list. This function is deprecated in Python >= 3 versions.
# Python code to demonstrate # Unzip a list of tuples # using map() # initializing list of tuples test_list = [('Akshat', 1), ('Bro', 2), ('is', 3), ('Placed', 4)] # Printing original list print ("Original list is : " + str(test_list)) # using map() to # perform Unzipping res = map(None, *test_list) # Printing modified list print ("Modified list is : " + str(res)) |
Output :
Original list is : [('Akshat', 1), ('Bro', 2), ('is', 3), ('Placed', 4)]
Modified list is : [('Akshat', 'Bro', 'is', 'Placed'), (1, 2, 3, 4)]
Recommended Posts:
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Find the tuples containing the given element from a list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Count tuples occurrence in list of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Combining tuples in list of tuples
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Convert list of tuples to list of strings
- Python | Convert list of strings to list of tuples
- Python | Convert list of tuples to list of list
- Python | Convert list of tuples into list
- Python | Update a list of tuples using another list
- Python | Min and Max value in list of tuples
- Python | Group tuples in list with same first value
- Python | Get unique tuples from list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



