Convert a List of Tuples into List in Python
Convert a List of Tuples into List in Python :
For Converting a list of tuples into list, in Python firstly given a list of tuples, write a Python program to convert it into a list. we can perform this operation using various Methods.
Example :
- a=[ ( 1, 2) , ( 3 ,4) ]
- a = [1,2,3,4 ]
Convert a List of Tuples into List
In Python, Tuples are similar to lists, but with one crucial difference: they are immutable. This means that once you’ve defined a tuple, its contents cannot be changed. But Converting a list of tuples into a list in Python is a straightforward process. Whether you choose list comprehension or the itertools module, the result will be the same
Below are methods to convert list of tuples into a list in Python .
Code 1 :
#Python Program # Python code to convert list of tuples into list # List of tuple initialization lt = [('A', 2), ('B', 4), ('C', '6')] # using list comprehension liout = [item for t in lt for item in t] # printing output print('The List is : ' , liout)
Output :
The List is : ['A', 2, 'B', 4, 'C', '6']
Code 2:
#Python Program # Python code to convert list of tuples into list import itertools # List of tuple tup = [('A', 2), ('B', 4), (5, 6)] # Using itertools liout = list(itertools.chain(*tup)) # printing output print("The list is :" ,liout)
Output :
The list is : ['A', 2, 'B', 4, 5, 6]
Code 3 :
#Python Program # Python code to convert list of tuples into list # List of tuple tup = [('A', 2), ('B', 4), (5, 6)] # Using itertools liout = [] for i in tup: for j in i: liout.append(j) # printing output print("The list is :" ,liout)
Output :
The list is : ['A', 2, 'B', 4, 5, 6]
Code 4 :
#Python Program # Python code to convert list of tuples into list # List of tuple tup = [('A', 2), ('B', 4), (5, 6)] # Using map for 0 index b = map(lambda x: x[0], tup) # Using map for 1 index c = map(lambda x: x[1], tup) # converting to list b = list(b) c = list(c) # Combining output liout = b + c print("The list is :" ,liout)
Output :
The list is : ['A', 'B', 5, 2, 4, 6]
Code 5 :
#Python Program # Python code to convert list of tuples into list # List of tuple tup = [('A', 2), ('B', 4), (5, 6)] # using sum function() liout = list(sum(tup, ())) print("The list is :" ,liout)
Output :
The list is : ['A', 2, 'B', 4, 5, 6]
Code 6 :
#Python Program # Python code to convert list of tuples into list import operator from functools import reduce # List of tuple tup = [('A', 2), ('B', 4), (5, 6)] print("The list is :" ,(list(reduce(operator.concat, tup))))
Output :
The list is : ['A', 2, 'B', 4, 5, 6]
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment