How To Convert A List Of Tuples To A List Of Lists In Python

Convert a list of tuples to a list of lists in Python

There are several ways to convert a list of tuples to a list of lists in Python, and each has its benefits. This post will show three easy ways to do that in detail.

Convert a list of tuples to a list of lists in Python

Using list comprehension and list() function

We’ve already covered the syntax of list comprehension here. You can refer to it if you want.

The list() function is one of the most basic and essential functions in Python. It allows you to create a list of items, which can be of any data type, including numbers, strings, tuples, and even other lists.

This way, to convert a list of tuples to a list of lists, we use list comprehension to generate a new list with the elements converted to a list using the list() function. As an example:

# Create a list of tuples
listOfTuples = [('Learn', 'Python'), ('at', 'Learn', 'Share', 'IT')]
print('List of tuples:', listOfTuples)

# Convert the tuples into lists using the list() function and list comprehension
listOfLists = [list(e) for e in listOfTuples]
print('List of lists:', listOfLists)

Output:

List of tuples: [('Learn', 'Python'), ('at', 'Learn', 'Share', 'IT')]
List of lists: [['Learn', 'Python'], ['at', 'Learn', 'Share', 'IT']]

Using map() function

The map() function applies a specified function to all elements in an iterable object(list, tuple, dictionary, set).

You can use the map() function to convert a list of tuples to a list of lists by passing the list function as the first argument and the list of tuples as the second argument. As an example:

# Create a list of tuples
listOfTuples = [('Learn', 'Python'), ('at', 'Learn', 'Share', 'IT')]
print('List of tuples:', listOfTuples)

# Convert the tuples into a map of lists using the map() function
mapOfLists = map(list, listOfTuples)

# Convert map object back to list object
listOfLists = list(mapOfLists)
print('List of lists:', listOfLists)

Output:

List of tuples: [('Learn', 'Python'), ('at', 'Learn', 'Share', 'IT')]
List of lists: [['Learn', 'Python'], ['at', 'Learn', 'Share', 'IT']]

Using For loop

Finally, you can convert a list of tuples to a list of lists using a For loop.

This time, we will iterate through all the elements of the list of tuples. In each loop, we use the list() function to convert them to a list and push them to an empty list using append(). Like this:

# Create a list of tuples
listOfTuples = [('Learn', 'Python'), ('at', 'Learn', 'Share', 'IT')]
print('List of tuples:', listOfTuples)

# Create an empty list
listOfLists = []

# Loop through each tuple in the listOfTuples
for element in listOfTuples:
    # Convert each tuple into a list and add it to the listOfLists
    listOfLists.append(list(element))

print('List of lists:', listOfLists)

Output:

List of tuples: [('Learn', 'Python'), ('at', 'Learn', 'Share', 'IT')]
List of lists: [['Learn', 'Python'], ['at', 'Learn', 'Share', 'IT']]

Summary

Converting a list of tuples to a list of lists has several benefits. It is easier to manipulate data, the code is more readable, and the execution time is faster. List comprehension is useful for quickly converting a list of tuples to a list of lists. Python has an advantage over other programming languages because of this syntax. We hope you like the article about how to convert a list of tuples to a list of lists in Python.

Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *