Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Convert list of tuples into list

Python | Convert list of tuples into list

Given a list of tuples, write a Python program to convert it into a list. 

Example

Input: [(11, 20), (13, 40), (55, 16)]
Output:[11, 20, 13, 40, 55, 16]

Input: [('Geeks', 2), ('For', 4), ('geek', '6')]
Output: ['Geeks', 2, 'For', 4, 'geek', '6']

Convert List of Tuples to List of Lists in Python Methods

Convert lists of tuples into a list using Loop

This code initializes an empty list out and iterates through each tuple in lt and each item in the tuple, appending them to out.

Python3




# List of tuple initialization
lt = [('Geeks', 2), ('For', 4), ('geek', '6')]
 
# using nested for loops
out = []
for t in lt:
    for item in t:
        out.append(item)
 
# printing output
print(out)
#This code is contributed by Vinay pinjala.


Output

['Geeks', 2, 'For', 4, 'geek', '6']

Convert lists of tuples into a list using List Comprehension 

The given Python code converts a list of tuples into a single list using list comprehension. It iterates through each tuple and each item within the tuple, flattening the list.

Python3




# Python code to convert list of tuples into list
 
# List of tuple initialization
lt = [('Geeks', 2), ('For', 4), ('geek', '6')]
 
# using list comprehension
out = [item for t in lt for item in t]
 
# printing output
print(out)


Output

['Geeks', 2, 'For', 4, 'geek', '6']

Convert lists of tuples into a list using sum()

This code uses the sum() in Python.sum() function treats the tuples as sequences and performs element-wise addition, effectively concatenating them.

Python3




# Python code to convert list of tuple into list
 
# List of tuple initialization
tup = [(1, 2), (3, 4), (5, 6)]
 
# using sum function()
out = list(sum(tup, ()))
 
# printing output
print(out)


Output

[1, 2, 3, 4, 5, 6]

Convert lists of tuples into list using Operator and Reduce

This code initializes a list of tuples named tup and then applies reduce() with the operator. concat to concatenate the tuples into a single sequence.

Python3




# Python code to convert list of tuple into list
 
import operator
from functools import reduce
 
# List of tuple initialization
tup = [(1, 2), (3, 4), (5, 6)]
 
# printing output
print(list(reduce(operator.concat, tup)))


Output

[1, 2, 3, 4, 5, 6]

Convert lists of tuples into list using lambda 

In this code, we are mapping the first and second values of the tuple of the list to the variables b and c and later we are typecasting the variable to the list and later we are combining the list.

Python3




# Python code to convert list of tuple into list
# List of tuple initialization
tup = [(1, 2), (3, 4), (5, 6)]
# Using map for 0 index
b = map(lambda x: x[0], tup)
# Using map for 1 index
c = map(lambda x: x[1], tup)
# converting to list
b = list(b)
c = list(c)
# Combining output
out = b + c
# printing output
print(out)


Output

[1, 3, 5, 2, 4, 6]

Convert lists of tuples into a list Using Enumerate function

The code uses a list comprehension with two nested loops. The outer loop iterates over each tuple i in tup while also tracking the index with enumerate(). The inner loop iterates over each element j in the tuple.

Python3




tup = [('1', '2'), ('3', '4'), ('5', '6')]
x=[int(j) for a,i in enumerate(tup) for j in i  ]
print(x)


Output

[1, 2, 3, 4, 5, 6]

Convert lists of tuples into lists using zip 

The code uses the zip() function to group the elements from the tuples together. By unpacking the zipped tuples using the * operator twice, the code extracts the elements from the tuples.

Python3




print(list(*zip(*[(1,), (2,), (3,)])))


Output

[1, 2, 3]

Convert lists of tuples into a list using eval 

This code uses list comprehension with nested loops to iterate through each tuple and each element within the tuple, applying the eval() function to convert the strings to numeric values.

Python3




tup = [('1', '2'), ('3', '4'), ('5', '6')]
x=[eval(j) for i in tup for j in i  ]
print(x)


Output

[1, 2, 3, 4, 5, 6]

Convert lists of tuples into list using Recursive Method 

The Python code defines a recursive function convert_to_list() that converts a list of tuples into a single list. The function takes a list of tuples as input and uses recursion to accumulate the elements of the tuples into a result list.

Python3




def convert_to_list(tuple_list, result=None):
    if result is None:
        result = []
    if len(tuple_list) == 0:
        return result
    else:
        result.extend(tuple_list[0])
        return convert_to_list(tuple_list[1:], result)
 
# List of tuple initialization
lt = [('Geeks', 2), ('For', 4), ('geek', '6')]
out = convert_to_list(lt)
# printing output
print(out)
#this code contributed by tvsk.


Output

['Geeks', 2, 'For', 4, 'geek', '6']

Convert lists of tuples into a list using itertools 

The provided Python code uses the itertools.chain() function to convert a list of tuples into a single list by concatenating the tuple elements. The resulting list is printed as the output.

Python3




# Python code to convert list of tuple into list
 
# Importing
import itertools
 
# List of tuple initialization
tuple = [(1, 2), (3, 4), (5, 6)]
 
# Using itertools
out = list(itertools.chain(*tuple))
 
# printing output
print(out)


Output

[1, 2, 3, 4, 5, 6]

RELATED ARTICLES

Most Popular

Recent Comments