Open In App

Python | Unzip a list of tuples

Improve
Improve
Like Article
Like
Save
Share
Report

The zipping technique, that is assigning a key value or pairing from two different lists has been covered in many articles before, sometimes we have a 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 performing this task of unzipping and is usually not used to perform this task, but a good method to start with. 

Python3




# 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




# 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))
 
# Performing unzipping
# using zip() and * operator
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() function 

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




# 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))
 
# Performing Unzipping
# using map() function
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)]

Method #4 : Using lists and tuple() method

Python3




# Python3 code to demonstrate
# unzip a list of tuples
 
# Initializing list of tuples
test_list = [('Akshat', 1), ('Bro', 2), ('is', 3), ('Placed', 4)]
 
# Printing original list
print("Original list is : " + str(test_list))
 
# Empty dictionary
res = []
 
a = []
b = []
 
for i in test_list:
    a.append(i[0])
    b.append(i[1])
 
res.append(tuple(a))
res.append(tuple(b))
 
# 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 5:Using re method

The given code uses regular expressions to extract the string and integer values from a list of tuples. It then creates a modified list consisting of two sublists – one containing the string values, and the other containing the integer values.

Steps:

  1. Import the re module to use regular expressions.
  2. Define a list of tuples lst.
  3. Convert the list of tuples into a string str_lst.
  4. Use the re.findall() function to extract all the string values from str_lst using the regular expression r”‘(.*?)'”. This regular expression matches any substring between two single quotes, including the quotes themselves, and returns the matched string as a group.
  5. Use the re.findall() function again to extract all the integer values from str_lst using the regular expression \d+. This regular expression matches any sequence of one or more digits.
  6. Use the list() function and map() function to convert the integer strings obtained in step 5 to actual integers.
  7. Create a modified list res by combining the two sublists obtained in steps 4 and 6.
  8. Print the original and modified lists.

Python3




import re
 
lst = [('Akshat', 1), ('Bro', 2), ('is', 3), ('Placed', 4)]
 
# Converting the list of tuples into a string
str_lst = str(lst)
res = [re.findall(r"'(.*?)'", str_lst),
       list(map(int, re.findall(r'\d+', str_lst)))]
 
# Printing original and modified list
print("Original list is:", lst)
print("Modified list is:", res)


Output

Original list is: [('Akshat', 1), ('Bro', 2), ('is', 3), ('Placed', 4)]
Modified list is: [['Akshat', 'Bro', 'is', 'Placed'], [1, 2, 3, 4]]

Time complexity: O(N) where n is the length of the input list of tuples. This is because the regular expressions used in the code need to scan the entire input string once to find all the matches.
Auxiliary Space: O(N), where n is the length of the input list of tuples. This is because the code creates a new string representation of the input list and two new lists to store the extracted string and integer values.



Last Updated : 10 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads