Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Flatten tuple of List to tuple

Improve Article
Save Article
  • Last Updated : 08 Feb, 2023
Improve Article
Save Article

Sometimes, while working with Python Tuples, we can have a problem in which we need to perform the flattening of tuples, which have listed as their constituent elements. This kind of problem is common in data domains such as Machine Learning. Let’s discuss certain ways in which this task can be performed.

Input : test_tuple = ([5], [6], [3], [8]) 
Output : (5, 6, 3, 8) 

Input : test_tuple = ([5, 7, 8]) 
Output : (5, 7, 8)

Method #1: Using sum() + tuple() The combination of above functions can be used to solve this problem. In this, we perform the task of flattening using sum(), passing empty list as its argument. 

Python3




# Python3 code to demonstrate working of
# Flatten tuple of List to tuple
# Using sum() + tuple()
 
# initializing tuple
test_tuple = ([5, 6], [6, 7, 8, 9], [3])
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Flatten tuple of List to tuple
# Using sum() + tuple()
res = tuple(sum(test_tuple, []))
 
# printing result
print("The flattened tuple : " + str(res))

Output

The original tuple : ([5, 6], [6, 7, 8, 9], [3])
The flattened tuple : (5, 6, 6, 7, 8, 9, 3)

Time complexity: O(n), where n is the total number of elements in all the lists of the tuple, because it has to traverse the lists in the tuple once to sum them up. 
Auxiliary space: O(n), because it needs to store all the elements in a new tuple.

Method #2 : Using tuple() + chain.from_iterable() The combination of above functions can be used to solve this problem. In this, we perform task of flattening using from_iterable() and conversion to tuple using tuple(). 

Python3




# Python3 code to demonstrate working of
# Flatten tuple of List to tuple
# Using tuple() + chain.from_iterable()
from itertools import chain
 
# initializing tuple
test_tuple = ([5, 6], [6, 7, 8, 9], [3])
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Flatten tuple of List to tuple
# Using tuple() + chain.from_iterable()
res = tuple(chain.from_iterable(test_tuple))
 
# printing result
print("The flattened tuple : " + str(res))

Output

The original tuple : ([5, 6], [6, 7, 8, 9], [3])
The flattened tuple : (5, 6, 6, 7, 8, 9, 3)

Time Complexity: O(n), where n is the total number of elements in all the lists in the tuple.
Auxiliary Space: O(n), as the memory space required to store the result is equal to the number of elements in the flattened tuple.

Method #3: Using extend() and tuple() methods

Python3




# Python3 code to demonstrate working of
# Flatten tuple of List to tuple
# initializing tuple
test_tuple = ([5, 6], [6, 7, 8, 9], [3])
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Flatten tuple of List to tuple
res=[]
for i in test_tuple:
    res.extend(i)
res=tuple(res)
# printing result
print("The flattened tuple : " + str(res))

Output

The original tuple : ([5, 6], [6, 7, 8, 9], [3])
The flattened tuple : (5, 6, 6, 7, 8, 9, 3)

Time complexity: O(n), where n is the number of elements in the input tuple.
Auxiliary space: O(n), as we are using an extra list to store the flattened tuple.

Method #4: Using Nested loops

Python3




# Python3 code to demonstrate working of
# Flatten tuple of List to tuple
# initializing tuple
test_tuple = ([5, 6], [6, 7, 8, 9], [3])
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Flatten tuple of List to tuple
res = []
for i in test_tuple:
    for j in i:
        res.append(j)
res = tuple(res)
# printing result
print("The flattened tuple : " + str(res))

Output

The original tuple : ([5, 6], [6, 7, 8, 9], [3])
The flattened tuple : (5, 6, 6, 7, 8, 9, 3)

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!