Convert List of Tuples to List of List in Python
Convert List of Tuples to List of List in Python:
Given a list of tuples, write a Python program to convert it into a list of list. we can perform this operation using various Methods.
Example :
- a=[ ( 1, 2) , ( 3 ,4) ]
- a = [ [1,2] ,[3,4 ] ]
Convert List of Tuples to List of List
Converting a list of tuples to a list of lists in Python is a common operation that allows you to transform your data structure for various purposes. This task is relatively straightforward and can be accomplished using a variety of methods.
Method 1 :
This can easily be achieved using the list comprehension. We just iterate through each tuple of the list convert the tuples to the list.
#Python Program # Python code to convert list of tuples into list of list # List of tuple tup = [('A', 2), ('B', 4), (5, 6)] print('The original list of tuple is :' , tup) # using list comprehension # convert list of tuples to list of list res = [list(ele) for ele in tup] print("The list of list :" ,res)
Output :
The orignal list of tuple is : [('A', 2), ('B', 4), (5, 6)]
The list of list : [['A', 2], ['B', 4], [5, 6]]
Method 2 :
We can use the map function and list operator to perform this particular task. The map function binds each tuple and converts it into a list.
#Python Program # Python code to convert list of tuples into list of list # List of tuple tup = [('A', 2), ('B', 4), (5, 6)] print('The orignal list of tuple is :' , tup) # using map() + list # convert list of tuples to list of list res = list(map(list, tup)) print("The list of list :" ,res)
Output :
The orignal list of tuple is : [('A', 2), ('B', 4), (5, 6)]
The list of list : [['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