Python | Convert list of tuples to list of list
This is a quite simple problem but can have a good amount of application due to certain constraints of python language. Because tuples are immutable, they are not easy to process whereas lists are always a better option while processing. Let’s discuss certain ways in which we can convert a list of tuples to list of list.
Method #1 : Using list comprehension
This can easily be achieved using the list comprehension. We just iterate through each list converting the tuples to the list.
# Python3 code to demonstrate # convert list of tuples to list of list # using list comprehension # initializing list test_list = [( 1 , 2 ), ( 3 , 4 ), ( 5 , 6 )] # printing original list print ( "The original list of tuples : " + str (test_list)) # using list comprehension # convert list of tuples to list of list res = [ list (ele) for ele in test_list] # print result print ( "The converted list of list : " + str (res)) |
The original list of tuples : [(1, 2), (3, 4), (5, 6)] The converted list of list : [[1, 2], [3, 4], [5, 6]]
Method #2 : Using map()
+ list
We can use the combination of map function and list operator to perform this particular task. The map function binds each tuple and converts it into list.
# Python3 code to demonstrate # convert list of tuples to list of list # using map() + list # initializing list test_list = [( 1 , 2 ), ( 3 , 4 ), ( 5 , 6 )] # printing original list print ( "The original list of tuples : " + str (test_list)) # using map() + list # convert list of tuples to list of list res = list ( map ( list , test_list)) # print result print ( "The converted list of list : " + str (res)) |
The original list of tuples : [(1, 2), (3, 4), (5, 6)] The converted list of list : [[1, 2], [3, 4], [5, 6]]
Recommended Posts:
- Python | Convert list of strings to list of tuples
- Python | Convert list of tuples to list of strings
- Python | Convert list of tuples into list
- Python | Convert string tuples to list tuples
- Python | Convert String to list of tuples
- Python | Convert list of tuples into digits
- Python | Convert a list of Tuples into Dictionary
- Python | Convert list elements to bi-tuples
- Python | Convert dictionary to 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 dictionary value lists
- Python | Convert list of string to list of list
- Python | Update a list of tuples using another list
- Python | Convert mixed data types tuple list to string list
- Python | Convert list of numerical string to list of Integers
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.