We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

84
5.326.238,90

Cart

Quantity 2.500,00
Quantity 3.500,00
Quantity 116,00
Quantity 3.000,00
Quantity 1.300,00
Quantity 3.500,00
Quantity 50,00
Quantity 1.600,00
Quantity 11.934,00
Quantity 9.500,00
Quantity 35,00
Quantity 5.700,00

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.

Advertisements

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.

84
5.326.238,90

Cart

Quantity 2.500,00
Quantity 3.500,00
Quantity 116,00
Quantity 3.000,00
Quantity 1.300,00
Quantity 3.500,00
Quantity 50,00
Quantity 1.600,00
Quantity 11.934,00
Quantity 9.500,00
Quantity 35,00
Quantity 5.700,00