Open In App

Python | Convert list of tuples into list

Last Updated : 01 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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]


Similar Reads

Python | Convert string tuples to list tuples
Sometimes, while working with Python we can have a problem in which we have a list of records in form of tuples in stringified form and we desire to convert them to a list of tuples. This kind of problem can have its occurrence in the data science domain. Let's discuss certain ways in which this task can be performed. Method 1 (Using eval() + list
4 min read
Python | Remove duplicate tuples from list of tuples
Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list. Examples: Input : [(1, 2), (5, 7), (3, 6), (1, 2)] Output : [(1, 2), (5, 7), (3, 6)] Input : [('a', 'z'), ('a', 'x'), ('z', 'x'), ('a', 'x'), ('z', 'x')] Output : [('a', 'z'), ('a', 'x'), ('z', 'x')] Method #1 : List comprehension This is a naive
5 min read
Python | Find the tuples containing the given element from a list of tuples
Given a list of tuples, the task is to find all those tuples containing the given element, say n. Examples: Input: n = 11, list = [(11, 22), (33, 55), (55, 77), (11, 44)] Output: [(11, 22), (11, 44)] Input: n = 3, list = [(14, 3),(23, 41),(33, 62),(1, 3),(3, 3)] Output: [(14, 3), (1, 3), (3, 3)] There are multiple ways we can find the tuples contai
6 min read
Python | Remove tuples from list of tuples if greater than n
Given a list of a tuple, the task is to remove all the tuples from list, if it's greater than n (say 100). Let's discuss a few methods for the same. Method #1: Using lambda STEPS: Initialize a list of tuples: ini_tuple = [('b', 100), ('c', 200), ('c', 45), ('d', 876), ('e', 75)]Print the initial list: print("intial_list", str(ini_tuple))Define the
6 min read
Python | Remove tuples having duplicate first value from given list of tuples
Given a list of tuples, the task is to remove all tuples having duplicate first values from the given list of tuples. Examples: Input: [(12.121, 'Tuple1'), (12.121, 'Tuple2'), (12.121, 'Tuple3'), (923232.2323, 'Tuple4')] Output: [(12.121, 'Tuple1'), (923232.2323, 'Tuple4')]Input: [('Tuple1', 121), ('Tuple2', 125), ('Tuple1', 135), ('Tuple4', 478)]
7 min read
Python | Count tuples occurrence in list of tuples
Many a time while developing web and desktop products in Python, we use nested lists and have several queries about how to find the count of unique tuples. Let us see how to get the count of unique tuples in the given list of tuples. Below are some ways to achieve the above task. Method #1: Using Iteration C/C++ Code # Python code to count unique #
5 min read
Python | Combining tuples in list of tuples
Sometimes, we might have to perform certain problems related to tuples in which we need to segregate the tuple elements to combine with each element of complex tuple element( such as list ). This can have application in situations we need to combine values to form a whole. Let's discuss certain ways in which this can be performed. Method #1: Using
7 min read
Python - Filter all uppercase characters Tuples from given list of tuples
Given a Tuple list, filter tuples that contain all uppercase characters. Input : test_list = [("GFG", "IS", "BEST"), ("GFg", "AVERAGE"), ("GfG", ), ("Gfg", "CS")] Output : [('GFG', 'IS', 'BEST')] Explanation : Only 1 tuple has all uppercase Strings. Input : test_list = [("GFG", "iS", "BEST"), ("GFg", "AVERAGE"), ("GfG", ), ("Gfg", "CS")] Output : [
8 min read
Python program to find Tuples with positive elements in List of tuples
Given a list of tuples. The task is to get all the tuples that have all positive elements. Examples: Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)] Output : [(4, 5, 9)] Explanation : Extracted tuples with all positive elements. Input : test_list = [(-4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)] Output : [] Explanation : No tuple wit
10 min read
Python program to find tuples which have all elements divisible by K from a list of tuples
Given a list of tuples. The task is to extract all tuples which have all elements divisible by K. Input : test_list = [(6, 24, 12), (60, 12, 6), (12, 18, 21)], K = 6 Output : [(6, 24, 12), (60, 12, 6)] Explanation : Both tuples have all elements multiple of 6. Input : test_list = [(6, 24, 12), (60, 10, 5), (12, 18, 21)], K = 5 Output : [(60, 10, 5)
7 min read