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

This tutorial will discuss about unique ways to convert a list of tuples to a list of lists in Python.

Table Of Contents

Method 1: Using List Comprehension

Iterate over all the tuples in list using List Comprehension. During iteration, apply list() method on each tuple to convert it into a list, then add that new list to a new main list constructed by List comprehension. In this end, List Comprehension will return a list of lists, constructed from list of tuples.

Let’s see the complete 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]]

Method 2: Using map() function

Apply list() function on each tuple in list, using the map() function. It will return a mapped object containing all the lists converted from tuples. Then join all those sublists into a new List. This way list of tuples will be converted into a list of lists.

Let’s see the complete 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]]

Method 3: Using enumerate() function

Create an empty list. Iterate over all tuples in list using a for-loop and enumerate() method. Then convert each tuple into a list, and add to the new empty list, created in first step. This way list of tuples will be converted into a list of lists.

Let’s see the complete 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]]

Summary

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

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