Convert a list of tuples to list of lists in Python

In this article, we will discuss different ways to create a list of lists from a list of tuples in python.

Table Of Contents

Method 1: Using List Comprehension

Inside a List Comprehension, iterate over all the tuples in list. During iteration, convert each tuple to a list, and append it to the new list created by list comprehension. Let’s see an example, where we have a list of tuples. Each tuple contains a student name, and his age. We will convert this list of tuples to list of lists. But the data will remain same. Let’s see an example,

# List of Tuples
listOfTuples = [('Mark', 34),
                ('John', 23),
                ('Ritu', 37),
                ('Avni', 29)]

# Convert a list of tuples to a list of lists using List Comprehension
listOfLists = [ list(tupleObj) for tupleObj in listOfTuples ]

print(listOfLists)

Output:

[['Mark', 34],
 ['John', 23],
 ['Ritu', 37],
 ['Avni', 29]]

Here, we have converted a list of tuples to a list of lists. The dataset in the nested lists is same as the data in tuples, only data structure is changed.

Method 2: Using for-loop

Create a empty list. Then, loop through all the tuples in the given list using a for loop. For each tuple, create a list with same content and add that to the empty list created in first step. Let’s see an example,

# List of Tuples
listOfTuples = [('Mark', 34),
                ('John', 23),
                ('Ritu', 37),
                ('Avni', 29)]

# Create empty list
listOfLists = []

# Convert a list of tuples to a list of lists using for loop
for tupleObj in listOfTuples:
    listOfLists.append(list(tupleObj))

print(listOfLists)

Output:

[['Mark', 34],
 ['John', 23],
 ['Ritu', 37],
 ['Avni', 29]]

Here, we have converted a list of tuples to a list of lists. The dataset in the nested lists is same as the data in tuples, only data structure is changed.

Method 3: Using map() function

Apply list() function on each tuple of the list using map() function. For this, we can pass this list() function, and given list of tuples as arguments to the map() function. It will apply the list() function on each element of the list i.e. on each tuple, and convert it to a list. All these converted lists will be stored in a mapped object. Then we can convert this mapped object to a list. This way we will get a list of lists from a list of lists. Let’s see an example,

# List of Tuples
listOfTuples = [('Mark', 34),
                ('John', 23),
                ('Ritu', 37),
                ('Avni', 29)]


# Convert a list of tuples to a list of lists using map() function
listOfLists = list(map(list, listOfTuples))

print(listOfLists)

Output:

[['Mark', 34],
 ['John', 23],
 ['Ritu', 37],
 ['Avni', 29]]

Here, we have converted a list of tuples to a list of lists. The dataset in the nested lists is same as the data in tuples, only data structure is changed.

Method 4: Using enumerate() function

Pass the list of tuples to the enumerate() function. It will yield pairs containing index position, and values from the list. Like, in our case it will yield pairs of index positions and tuples at those indices. Then we can convert these tuples to lists, and append them to a new list. Let’s see an example,

# List of Tuples
listOfTuples = [('Mark', 34),
                ('John', 23),
                ('Ritu', 37),
                ('Avni', 29)]

listOfLists = []

# Convert a list of tuples to a list of lists using enumerate() function
for index, tupObj in enumerate(listOfTuples):
    listOfLists.append(list(tupObj))

print(listOfLists)

Output:

[['Mark', 34],
 ['John', 23],
 ['Ritu', 37],
 ['Avni', 29]]

Here, we have converted a list of tuples to a list of lists.

Summary

We learned about different ways to convert list of tuples to list of lists in Python. Thanks.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top