How to convert a list of tuples to a list of lists in python? A list of tuples is often used to group related values together, and converting it to a list of lists can be useful for further data processing or analysis.
You can convert the list of tuples to a list of lists in python by using many ways, for example, using list comprehension
, enumerate
, for loop
, and map()
functions. In this article, I will explain convert the list of tuples to a list of lists in python by using all these methods with examples.
1. Quick Examples of Convert List of Tuples to List of Lists
If you are in a hurry, below are some quick examples of how to convert a list of tuples to a list of lists.
# Quick examples of convert list of tuples to list of lists
# Example 1: Convert list of tuples to list of list
# Using list comprehension
tuples = [(2500, 'Hadoop'), (2200, 'Spark'), (3000, 'Python')]
result = [list(t) for t in tuples]
# Example 2: Using enumerate function
tuples = [(12, 23), (15, 34), (25, 46)]
result = [list(t) for i,t in enumerate(tuples)]
# Example 3: Using map() function
# list() constructor
tuples = [(22, 13), (17, 46), (45, 58)]
result = list(map(list, tuples)
# Example 4: Using a For Loop
tuples = [(3, 5), (7, 9), (11, 13), (15, 17)]
list_of_lists = []
for tup in tuples:
list_of_lists.append(list(tup))
2. Convert List of Tuples to List of Lists Using List Comprehension
You can convert a list of tuples to a list of lists using list comprehension in Python. For example, you first define a list of tuples called tuples
. you then use a list comprehension to iterate over each tuple in the list and convert it to a list using the list()
function. The resulting list is then added to the new list called result.
# initializing list
tuples = [(2500, 'Hadoop'), (2200, 'Spark'), (3000, 'Python')]
print("Orginal list of tuples: ",tuples)
# Convert list of tuples to list of list
# Using list comprehension
result = [list(t) for t in tuples]
print("Updated list of list: ",result)
# Output:
# Original list of tuples : [(2500, 'Hadoop'), (2200, 'Spark'), (3000, 'Python')]
# Updated list of list : [[2500, 'Hadoop'], [2200, 'Spark'], [3000, 'Python']]
3. Convert List of Tuples to List of Lists Using enumerate Function
You can also convert a list of tuples to a list of lists using the enumerate function, you can use a list comprehension with enumerate to loop through the list of tuples, unpack the tuple into separate values, and append them to a new list as a list.
# initializing list
tuples = [(12, 23), (15, 34), (25, 46)]
print("Orginal list of tuples: ",tuples)
# Convert list of tuples to list of list
# Using enumerate function
result = [list(t) for i,t in enumerate(tuples)]
print("Updated list of list: ",result)
# Output
# Orginal list of tuples: [(12, 23), (15, 34), (25, 46)]
# Updated list of list: [[12, 23], [15, 34], [25, 46]]
4. Using map() Function and the list() Constructor
You can use the map() function and the list()
constructor to convert. For example, you use the map()
function to apply the list()
constructor to each tuple in the tuples
list, The resulting map object is then converted to a list using the list()
constructor.
# initializing tuples list
tuples = [(22, 13), (17, 46), (45, 58)]
print("Orginal list of tuples: ",tuples)
# Using map() function and the list() constructor
result = list(map(list, tuples))
print("Updated list of list: ",result)
# Output
# Orginal list of tuples: [(22, 13), (17, 46), (45, 58)]
# Updated list of list: [[22, 13], [17, 46], [45, 58]]
5. Convert List of Tuples to List of Lists Using a For Loop
You can convert a list of tuples to a list of lists using a for loop
. For example, you loop through each tuple in the original list and use the list()
constructor to convert each tuple to a list. You can then append the resulting list to a new list called list_of_lists
.
# Using a for loop
tuples = [(3, 5), (7, 9), (11, 13), (15, 17)]
print("Orginal list of tuples: ",tuples)
# result list initialization
list_of_lists = []
for tup in tuples:
list_of_lists.append(list(tup))
print("Updated list of list: ",list_of_lists)
# Output
# Orginal list of tuples: [(3, 5), (7, 9), (11, 13), (15, 17)]
# Updated list of list: [[3, 5], [7, 9], [11, 13], [15, 17]]
Conclusion
In this article, I have explained how to convert a list of tuples to a list of lists in python by using list comprehension
, enumerate
, for loop
, and map()
functions with examples.
Happy Learning !!
Related Articles
- Find the Length of a List in Python
- Append List to Another List in Python
- Python Add Two Lists By Index Wise
- Python Remove from List by Index
- Python Sort List of Lists
- Python Append Suffix to List of Strings
- Python Append Prefix to List of Strings
- Singly Linked List in Python with Examples
- Python Catch Multiple Exceptions
- Python Return Tuple from Function
- Python Tuple index() Method
- Python Append List to a List Example
- Python Difference between List append() vs extend()
- Python Tuple Comparison