Open In App

Python | Convert list of tuples into list

Last Updated : 26 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python we often need to convert a list of tuples into a flat list, especially when we work with datasets or nested structures. In this article, we will explore various methods to Convert a list of tuples into a list.

Using itertools.chain()

itertools.chain() is the most efficient way to flatten a list of tuples. itertools.chain() takes multiple iterable (like tuples) and combines them into a single iterable without creating intermediate lists. The unpacking operator (*) can pass each tuple from the list to chain().

Python
import itertools

# Initial list of tuples
a = [('Geeks', 1), ('For', 2), ('geek', '3')]

# Flattening the list using itertools.chain
result = list(itertools.chain(*a))

print(result)

Output
['Geeks', 1, 'For', 2, 'geek', '3']

Other methods that we can use to convert a list of tuples into a flat list in Python are:

Using List Comprehension

List comprehension is a clean and Pythonic way to flatten a list of tuples. We use two for loops inside a single list comprehension. The first loop iterates through each tuple in the list. The second loop iterates through each element inside the current tuple and adds it to the result list.

Python
# Initial list of tuples
a = [('Geeks', 1), ('For', 2), ('geek', '3')]

# Flattening the list using list comprehension
result = [item for t in a for item in t]

print(result)

Output
['Geeks', 1, 'For', 2, 'geek', '3']

Using Loop

Using a loop to flatten a list of tuples is the most straightforward method.. We initialize an empty list to store the result. Then, we loop through each tuple in the original list. For each tuple, we add its elements to the result list using the extend() method, which appends each item from the tuple.

Python
# Initial list of tuples
a = [('Geeks', 1), ('For', 2), ('geek', '3')]

# Empty list to store the result
result = []

# Loop through each tuple in the list
for t in a:
    # Add each element of the tuple to the result list
    result.extend(t)

print(result)

Output
['Geeks', 1, 'For', 2, 'geek', '3']

Using functools.reduce() with lambda

reduce() function from the functools module can also flatten a list of tuples. reduce() applies a function (in this case, concatenation) to combine all elements in the list. It starts with the first two items and combines them, then combines the result with the next item, and so on.

Python
from functools import reduce

# Initial list of tuples
a = [('Geeks', 1), ('For', 2), ('geek', '3')]

# Flattening the list using reduce()
result = reduce(lambda x, y: x + y, a)

print(result)

Output
('Geeks', 1, 'For', 2, 'geek', '3')

Using numpy.flatten()

If we are working with numerical data and using the numpy library, we can use the flatten() method to convert a list of tuples into a flat list. First, we convert the list of tuples into a numpy array. Then, we use the flatten() method to get a 1D array, which we can convert to a list.

Python
import numpy as np

# Initial list of tuples
a = [('Geeks', 1), ('For', 2), ('geek', '3')]

# Convert to numpy array and flatten
result = np.array(a).flatten().tolist()

print(result)

Output
['Geeks', '1', 'For', '2', 'geek', '3']

Next Article

Similar Reads