Python – Remove nested records from tuple
Last Updated :
27 Apr, 2023
Sometimes, while working with records, we can have a problem in which an element of a record is another tuple records and we might have to remove the nested records. This is a problem which does not occur commonly, but having a solution to it is useful. Let’s discuss certain way in which this task can be performed.
Method 1: Using loop + isinstance() + enumerate()
This problem can be solved using the above functionalities. In this, we just loop through the elements using enumerate() to get the index count of it and check the type using isinstance() and recreate the new tuple by checking ignoring tuple records.
Python3
test_tup = ( 1 , 5 , 7 , ( 4 , 6 ), 10 )
print ( "The original tuple : " + str (test_tup))
res = tuple ()
for count, ele in enumerate (test_tup):
if not isinstance (ele, tuple ):
res = res + (ele, )
print ( "Elements after removal of nested records : " + str (res))
|
Output
The original tuple : (1, 5, 7, (4, 6), 10)
Elements after removal of nested records : (1, 5, 7, 10)
Time Complexity: O(n), where n is the length of the list test_tup
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method 2 : Using type() method
Python3
test_tup = ( 1 , 5 , 7 , ( 4 , 6 ), 10 )
print ( "The original tuple : " + str (test_tup))
res = []
for i in test_tup:
if not type (i) is tuple :
res.append(i)
res = tuple (res)
print ( "Elements after removal of nested records : " + str (res))
|
Output
The original tuple : (1, 5, 7, (4, 6), 10)
Elements after removal of nested records : (1, 5, 7, 10)
Time complexity: O(n), where n is the number of elements in the tuple.
Auxiliary Space: O(n), where n is the number of elements in the tuple.
Method 3 : Using filter()+lambda functions
Python3
test_tup = ( 1 , 5 , 7 , ( 4 , 6 ), 10 )
print ( "The original tuple : " + str (test_tup))
res = list ( filter ( lambda x: not isinstance (x, tuple ), test_tup))
print ( "Elements after removal of nested records : " + str (res))
|
Output
The original tuple : (1, 5, 7, (4, 6), 10)
Elements after removal of nested records : [1, 5, 7, 10]
Time Complexity:O(N)
Auxiliary Space: O(N)
Method 4: Using list comprehension
Python3
test_tup = ( 1 , 5 , 7 , ( 4 , 6 ), 10 )
print ( "The original tuple : " + str (test_tup))
res = [x for x in test_tup if not isinstance (x, tuple )]
print ( "Elements after removal of nested records : " + str (res))
|
Output
The original tuple : (1, 5, 7, (4, 6), 10)
Elements after removal of nested records : [1, 5, 7, 10]
Time Complexity: O(N)
Auxiliary Space: O(N)
Python3
from functools import reduce
test_tup = ( 1 , 5 , 7 , ( 4 , 6 ), 10 )
print ( "The original tuple : " + str (test_tup))
res = reduce ( lambda acc, x: acc + (x,) if not isinstance (x, tuple ) else acc, test_tup, ())
print (res)
|
Output
The original tuple : (1, 5, 7, (4, 6), 10)
(1, 5, 7, 10)
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 5: Method 5: Using itertools.chain()
Use the itertools.chain() function to flatten the nested tuple and create a new tuple without the inner tuples.
Python3
import itertools
test_tup = ( 1 , 5 , 7 , ( 4 , 6 ), 10 )
print ( "The original tuple: " + str (test_tup))
res = tuple (itertools.chain( * ([x] if not isinstance (x, tuple ) else x for x in test_tup)))
print (res)
|
Output
The original tuple: (1, 5, 7, (4, 6), 10)
(1, 5, 7, 4, 6, 10)
Time complexity: O(n), where n is the number of elements in the tuple.
Auxiliary space: O(n), as it creates a new tuple of the same length as the original tuple.
Method 6: using recursion
- Define a function flatten_tuple that takes a tuple as an argument.
- Create an empty list result.
- For each element elem in the input tuple, check if it is a tuple using isinstance(elem, tuple).
- If elem is not a tuple, append it to the result list.
- If elem is a tuple, recursively call the flatten_tuple function on it and extend the result list with the flattened tuple.
- Return the flattened result list as a tuple.
Python3
def flatten_tuple(tup):
result = []
for elem in tup:
if not isinstance (elem, tuple ):
result.append(elem)
else :
result.extend(flatten_tuple(elem))
return tuple (result)
test_tup = ( 1 , 5 , 7 , ( 4 , 6 ), 10 )
print ( "The original tuple: " + str (test_tup))
res = flatten_tuple(test_tup)
print (res)
|
Output
The original tuple: (1, 5, 7, (4, 6), 10)
(1, 5, 7, 4, 6, 10)
Time complexity: O(n), where n is the length of given test_tup
Auxiliary space: O(n) since it creates a new list to store the flattened elements.
Please Login to comment...