How to convert a list of tuples into a list in Python? A list of tuples means a list containing tuples as elements and converting this to a list is required for further data processing or analysis.
You can convert a list of tuples into a list in Python by using many ways, for example, using list comprehension, iteration, itertools, sum(), lambda, reduce(), enumerate(), zip(), nested for loops, and eval() functions. In this article, I will explain converting a list of tuples into a list by using all these methods with examples.
1. Quick Examples of Convert List of Tuples into List
If you are in a hurry, below are some quick examples of how to convert a list of tuples into a list.
# Quick examples of convert a list of tuples into a list
# Example 1: Using list comprehension
tuples = [(2500, 'Hadoop'), (2200, 'Spark'), (3000, 'Python')]
result = [item for t in tuples for item in t]
# Example 2: Convert list of tuples into list
# Using iteration
tuples = [(3, 5), (7, 9), (11, 13), (15, 17)]
flat_list = []
for tup in tuples:
for x in tup:
flat_list.append(x)
# Example 3: Convert list of tuples into list
# Using itertools
tuples = [(0, 2), (4, 6), (8, 10), (12, 14)]
result = list(itertools.chain(*tuples))
# Example 4: Convert list of tuples into list
# Using sum() function
tuples = [(0, 5), (10, 15), (20, 25), (30, 35)]
print("Orginal list of tuples: ",tuples)
result = list(sum(tuples, ()))
# Example 5: Convert list of tuples into list using lambda
tuples = [(2500, 'Spark'), (2200, 'Hadoop'), (3000, 'Python')]
x = map(lambda x: x[0], tuples)
y = map(lambda x: x[1], tuples)
x = list(x)
y = list(y)
result = x + y
# Example 6: Convert list of tuples into list
# using operator and reduce
tuples = [(2500, 'Spark'), (2200, 'Hadoop'), (3000, 'Python')]
result = list(reduce(operator.concat, tuples))
# Example 7: Using enumerate function
tuples = [('0', '2'), ('4', '6'), ('8', '9')]
result =[int(j) for a,i in enumerate(tuples) for j in i]
# Example 8: Using zip
result = list(*zip(*[(3,), (6,), (9,)]))
# Example 9: Using nested for loops
tuples = [(2500, 'Spark'), (2200, 'Hadoop'), (3000, 'Python')]
new_list = []
for tup in tuples:
for item in tup:
new_list.append(item)
# Example 10: Convert list of tuples into list
# Using eval
tuples = [('0', '3'), ('7', '9'), ('14', '17')]
result = [eval(j) for i in tuples for j in i ]
<a id="h-2-convert-list-of-tuples-to-list-using-comprehension"></a>
2. Convert List of Tuples to List Using Comprehension
You can use list comprehension to convert a list of tuples into a flat list of elements in Python. List comprehension is a concise and elegant way of creating a new list by iterating over an existing list or iterable. For example, you have a list of tuples with two elements each. you want to convert this into a flat list with all the elements.
# List of tuple initialization
tuples = [(2500, 'Hadoop'), (2200, 'Spark'), (3000, 'Python')]
print("Orginal list of tuples: ",tuples)
# Using list comprehension
result = [item for t in tuples for item in t]
print("Convert list of tuples into list: ",result)
# Output
# Orginal list of tuples: [(2500, 'Hadoop'), (2200, 'Spark'), (3000, 'Python')]
# Convert list of tuples into list: [2500, 'Hadoop', 2200, 'Spark', 3000, 'Python']
Here, the expression inside the square brackets is [item for t in tuples for item in t], which means "for each tuple t in the list of tuples, for each item in the tuple, add the item to the new list". The result is a new list that contains all the elements from the original list of tuples.
<a id="h-3-convert-list-of-tuples-into-list-using-iteration"></a>
3. Convert List of Tuples into List Using iteration
You can convert a list of tuples into a list of elements using iteration. Here, first you initialize an empty list called flat_list, and then use a nested for loop to iterate over each tuple in the list of tuples, and then iterate over each item in the tuple. For each item, we append it to the flat_list.
# Convert list of tuples into list using iteration
tuples = [(3, 5), (7, 9), (11, 13), (15, 17)]
print("Orginal list of tuples: ",tuples)
# result list initialization
flat_list = []
for tup in tuples:
for x in tup:
flat_list.append(x)
print("Convert list of tuples into list: ",flat_list)
# Output
# Orginal list of tuples: [(3, 5), (7, 9), (11, 13), (15, 17)]
# Convert list of tuples into list: [3, 5, 7, 9, 11, 13, 15, 17]
<a id="h-4-convert-list-of-tuples-into-list-using-itertools"></a>
4. Convert List of Tuples into List Using itertools
You can use the itertools module to convert a list of tuples into a list of elements. The itertools module provides a number of functions for efficient looping, and one of these functions is chain(), which can be used to chain together multiple iterables into a single iterable.
import itertools
tuples = [(0, 2), (4, 6), (8, 10), (12, 14)]
print("Orginal list of tuples: ",tuples)
# Convert list of tuples into list
# Using itertools
result = list(itertools.chain(*tuples))
print("Convert list of tuples into list: ",result)
# Output
# Orginal list of tuples: [(0, 2), (4, 6), (8, 10), (12, 14)]
# Convert list of tuples into list: [0, 2, 4, 6, 8, 10, 12, 14]
Here, you pass the list of tuples tuples to the chain() function, and use the unpacking operator * to unpack the tuples and pass each tuple as a separate argument to the function. The chain() function then chains together all the elements in the tuples, and returns an iterable. you convert this iterable into a list using the list() function, which gives us the final list of elements.
<a id="h-5-using-sum-function"></a>
5. Using sum() Function
The sum() function is typically used to add up numbers, but it can also be used to concatenate strings, lists, and other iterables. For example, you start with a list of tuples with two elements each. You pass the list of tuples tuples as the first argument to the sum() function. You also pass an empty tuple as the second argument, which tells the sum() function to start with an empty list. The sum() function then concatenates all the tuples together and returns a single tuple. You then convert this tuple into a list using thelist()function.
# Convert list of tuples into list using sum() function
tuples = [(0, 5), (10, 15), (20, 25), (30, 35)]
print("Orginal list of tuples: ",tuples)
result = list(sum(tuples, ()))
print("Convert list of tuples into list: ",result)
# Output
# Orginal list of tuples: [(0, 5), (10, 15), (20, 25), (30, 35)]
# Convert list of tuples into list: [0, 5, 10, 15, 20, 25, 30, 35]
<a id="h-6-convert-list-of-tuples-into-list-using-lambda"></a>
6. Convert List of Tuples into List Using Lambda
You can convert a list of tuples into a list using lambda function , you can use the map() function along with a lambda function to extract the first and second elements from each tuple.
# Convert list of tuples into list using lambda
tuples = [(2500, 'Spark'), (2200, 'Hadoop'), (3000, 'Python')]
# Using map for 0 index
x = map(lambda x: x[0], tuples)
# Using map for 1 index
y = map(lambda x: x[1], tuples)
# converting to list
x = list(x)
y = list(y)
result = x + y
print(result)
# Output
# [2500, 2200, 3000, 'Spark', 'Hadoop', 'Python']
<a id="h-7-using-operator-and-reduce"></a>
7. Using Operator and Reduce
Similarly, to convert a list of tuples into a list use the operator and reduce, you can use the reduce() function to apply a binary operator to the elements of the iterable and accumulate the results. In this case, you can use the operator.concat() function to concatenate the tuples and create a single list.
import operator
from functools import reduce
# Convert list of tuples into list
# using operator and reduce
tuples = [(2500, 'Spark'), (2200, 'Hadoop'), (3000, 'Python')]
result = list(reduce(operator.concat, tuples))
print(result)
# Output
# [2500, 'Spark', 2200, 'Hadoop', 3000, 'Python']
<a id="h-8-using-enumerate-function"></a>
8. Using enumerate Function
You can convert a list of tuples into a list using the enumerate() function. For examples,
# Using enumerate function
tuples = [('0', '2'), ('4', '6'), ('8', '9')]
result =[int(j) for a,i in enumerate(tuples) for j in i]
print(result)
# Output
# [0, 2, 4, 6, 8, 9]
<a id="h-9-convert-list-of-tuples-into-list-using-zip"></a>
9. Convert List of Tuples into List Using zip
Let's convert using the zip() function. For example, to convert a list of one-element tuples into a flattened list of integers using the zip() function and the unpacking operator *.
# Using zip
result = list(*zip(*[(3,), (6,), (9,)]))
print(result)
<a id="h-10-using-nested-for-loops"></a>
10. Using Nested for Loops
You can also convert a list of tuples into a list using nested for loops. For example, tuples is a list of tuples. You use a nested for loop to iterate over each tuple in the list, and then over each item in the tuple. The item is then appended to the new_list using the list.append() method. The result new_list will be a list of items from the original tuples. The order of the items in new_list will is the same as the order of the items in the original tuples.
# List of tuple initialization
tuples = [(2500, 'Spark'), (2200, 'Hadoop'), (3000, 'Python')]
# Using nested for loops
new_list = []
for tup in tuples:
for item in tup:
new_list.append(item)
print(new_list)
# Output
# [2500, 'Spark', 2200, 'Hadoop', 3000, 'Python']
<a id="h-11-convert-list-of-tuples-into-list-using-eval"></a>
11. Convert List of Tuples into List Using eval
You can also convert a list of tuples into a list using eval() function, for example,
# Convert list of tuples into list using eval
tuples = [('0', '3'), ('7', '9'), ('14', '17')]
result = [eval(j) for i in tuples for j in i ]
print(result)
# Output
# [0, 3, 7, 9, 14, 17]
Conclusion
In this article, I have explained how to convert a list of tuples into a list in Python by using list comprehension, iteration, itertools, sum(), lambda, reduce(), enumerate(), zip, nested for loops, and eval() functions with examples.
Happy Learning !!
Related Articles
- Convert Tuple to String in Python
- Convert String to List of Tuples in Python
- Convert String Tuples to List Tuples Python
- Convert Tuple to List in Python
- Convert List to Tuple in Python
- Convert Tuples to Dictionary in Python
- Flatten List of Tuples to String in Python
- Convert List of Lists to Tuple of Tuples in Python
- Concatenate String and Int in Python
- Convert a Nested List into a Flat List in Python
