Python | Convert list of tuples into list
Last Updated :
26 Dec, 2024
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.
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']
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']
Similar Reads
Python - Convert a list into tuple of lists When working with data structures in Python, there are times when we need to convert a list into a tuple of smaller lists.For example, given a list [1, 2, 3, 4, 5, 6], we may want to split it into a tuple of two lists like ([1, 2, 3], [4, 5, 6]). We will explore different methods to achieve this con
3 min read
Python | Convert list of tuples to list of list Converting list of tuples to list of lists in Python is a task where each tuple is transformed into list while preserving its elements. This operation is commonly used when we need to modify or work with the data in list format instead of tuples.Using numpyNumPy makes it easy to convert a list of tu
3 min read
Python | Convert list of tuples into digits Given a list of tuples, the task is to convert it into list of all digits which exists in elements of list. Letâs discuss certain ways in which this task is performed. Method #1: Using re The most concise and readable way to convert list of tuple into list of all digits which exists in elements of l
6 min read
Convert List of Dictionary to Tuple list Python Given a list of dictionaries, write a Python code to convert the list of dictionaries into a list of tuples.Examples: Input: [{'a':[1, 2, 3], 'b':[4, 5, 6]}, {'c':[7, 8, 9], 'd':[10, 11, 12]}] Output: [('b', 4, 5, 6), ('a', 1, 2, 3), ('d', 10, 11, 12), ('c', 7, 8, 9)] Below are various methods to co
5 min read
Python | Convert Integral list to tuple list Sometimes, while working with data, we can have a problem in which we need to perform type of interconversions of data. There can be a problem in which we may need to convert integral list elements to single element tuples. Let's discuss certain ways in which this task can be performed. Method #1 :
3 min read