Convert a list of tuples to a list in Python

In this article, we will discuss different ways to convert a list of tuples into a list in Python.

Table Of Contents

Method 1: Using List Comprehension

Inside a list comprehension, iterate over all tuples in the given list, and for each tuple iterate through all elements in it. During iteration, add each element of each tuple to the new list (using list comprehension). It will give us a flat list. Let’s see an example,

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

# Convert list of tuples to a list using List Comprehension
listObj = [ elem for detail in listOfTuples for elem in detail ]

print(listObj)

Output:

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

We created a flat list from a list of tuples.

Advertisements

Method 2: Using nested for-loops

First of all, create an empty list. Then use a for-loop to iterate through all the tuples in the given list. During iteration, for each tuple run another for loop, to iterate over all elements of that tuple. Inside this nested for loop, add each individual element to the new list created in first step. let’s see an example,

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

# create an empty list
listObj = []

# Convert list of tuples to a list using nested for-loop
for detail in listOfTuples:
    for elem in detail:
        listObj.append(elem) 

print(listObj)

Output:

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

We created a flat list from a list of tuples.

Method 3: Using itertools and chain() function

Apply astrik to the list of tuples, it will diffuse the list elements and gives individual tuples. Then pass the iterable tuples to the chain() function of itertools module. It returns the elements from the first tuple until it is exhausted, then elements from the next tuple, until all of the tuples are exhausted. We can construct a list from all the individual elements returned by the function chain(). Let’s see an example,

import itertools

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

# Convert list of tuples to a list using itertools module
listObj = list(itertools.chain(*listOfTuples))

print(listObj)

Output:

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

We created a flat list from a list of tuples.

Method 4: Using sum() function

Pass the list of tuples to sum() function, and create a list from the returned values. Let’s see an example,

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

# Convert list of tuples to a list using sum() function
listObj = list(sum(listOfTuples, ()))

print(listObj)

Output:

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

We created a flat list from a list of tuples.

Method 5: Using reduce() function

Reduce the list of tuples into a flat list by passing the operator.concat() and list of tuples as arguments to the reduce() function. It will apply the operator.concat() on each tuple in the list, and create a new sequence of individual items.

import operator
from functools import reduce

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

# Convert list of tuples to a list using reduce() function
listObj = list(reduce(operator.concat, listOfTuples))

print(listObj)

Output:

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

We created a flat list from a list of tuples.

Method 6: Using map() and Lambda function

Using map() function create a list of first value of each tuple from the list of tuples. Then using the same logic, create a list of second value of each tuple from the list of tuples. These merge these two lists to create a flat list of items.

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

# Convert list of tuples to a list using map() function
listObj =   list(map(lambda x: x[0], listOfTuples)) + \
            list(map(lambda x: x[1], listOfTuples))

print(listObj)

Output:

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

We created a flat list from a list of tuples.

Summary

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

Advertisements

Thanks for reading.

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