Convert a List of Tuples to a List in Python

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

Table Of Contents

Method 1: Using List Comprehension

We are going to use two nested for loops inside List Comprehension.

First, loop through all the tuples in list using List Comprehension. Then for each tuple, iterate over all the elements in that tuple using another for-loop inside List comprehension. During iteration, add every element of each tuple into the new list constructed by List comprehension.

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

Method 2: Using itertools chain()

The itertools.chain() function accepts multiple iterable sequences as arguments, and chains them together.

It returns a chain object, which is like an iterator. When we iterate over this chained object, it returns elements from the all the chained iterables one by one, until all of the iterables are exhausted.

So, apply astrik on list to decouple all the tuples in list as individual arguments, and pass them to the itertools.chain() function. Then convert the returned chained object to list, to store all elements of each tuple in a list. This way we can convert a list of tuples into a list.

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

Method 3: Using reduce() function

Pass operator.concat method and the list to the reduce() function as parameters. It will apply the concat() function on two tuples from given list cumulatively to a new sequence, from left to right. Basically it will return a new sequence containing all the items from tuples. Then, convert that sequence to a new list. This way we can convert a list of tuples into a list.

Let’s see the complete example,

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]

Summary

We learned about three different ways to convert a list of tuples into a 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