Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Unzip a List of tuples in Python

Unzip a List of tuples in Python: 

Zip: The zip function returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second iterator item in each passed iterator are paired together etc. 

Unzip: Unzip means converting the zipped values or nested comprehension back to linear form or according to our need, This is done with the help of “*” operator

unzip of list of tuple in Python
  • Let’s understand this topic with some examples to unzip a list of tuples. 

Method #1:

  • This is the most basic approach to unzip a list of tuples. It does not need a pre-defined function or library to perform this task, basic knowledge of python is sufficient.
#python Program
#Rishikesh 
# Unzip a list of tuples 
# using list comprehension 

# initializing list of tuples 
list_tup = [(‘PrepInsta’1), (‘is’2), (‘here’3), (‘for’4),(‘help’,5)] 

# Printing original list 
print (“Original list of tup:-> “ ,*(list_tup),sep=‘ ‘

# using list comprehension to 
# perform Unzipping 
res_list = [[ i for i, j in list_tup ], 
    [ j for i, j in list_tup ]] 
    
# Printing modified list 
print (“Modified list is:->” ,*res_list,sep=‘ ‘

Output :

Original list of tup:-> ('PrepInsta', 1) ('is', 2) ('here', 3) ('for', 4) ('help', 5)
Modified list is:-> ['PrepInsta', 'is', 'here', 'for', 'help'] [1, 2, 3, 4, 5]

Method #2: 

  • Using map keyword, we can perform this task. 
  • This also uses the * operator to perform the basic unpacking of the list. 
  • This method is not for Python3 and above.
#python Program
#Rishikesh 
# Unzip a list of tuples 
# using list comprehension 

# initializing list of tuples 
list_tup = [(‘PrepInsta’1), (‘is’2), (‘here’3), (‘for’4),(‘help’,5)] 

# Printing original list 
print (“Original list of tup:-> “ + str(list_tup)) 

# using map() to 
# perform Unzipping 
res = map(None, *list_tup) 
    
# Printing modified list 
print (“The final list is :-> “ + str(res)) 

Output :

Original list of tup:-> [('PrepInsta', 1) ('is', 2) ('here', 3) ('for', 4) ('help', 5)]
Modified list is:-> [['PrepInsta', 'is', 'here', 'for', 'help'] [1, 2, 3, 4, 5]]

Method #3 

  • This is the most popular and recommended method to perform this operation. 
  • Zip function is used to perform this operation.
#python Program
#Rishikesh 
# Unzip a list of tuples 
# using zip funtion

# initializing list of tuples 
list_tup = [(‘PrepInsta’1), (‘is’2), (‘here’3), (‘for’4),(‘help’,5)] 

# Printing original list 
print (“Original list of tup:-> “ + str(list_tup)) 

# using map() to 
# perform Unzipping 
res = list(zip(*list_tup)) 
    
# Printing modified list 
print (“The final list is :-> “ + str(res)) 

Output :

Original list of tup:-> [('PrepInsta', 1) ('is', 2) ('here', 3) ('for', 4) ('help', 5)]
Modified list is:-> [['PrepInsta', 'is', 'here', 'for', 'help'] [1, 2, 3, 4, 5]]

These are the methods to unzip a list of tuples in Python, it is similar to unpacking of tuples