Python – Remove nested records from tuple
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
# Python3 code to demonstrate working of # Remove nested records # using isinstance() + enumerate() + loop # initialize tuple test_tup = ( 1 , 5 , 7 , ( 4 , 6 ), 10 ) # printing original tuple print ("The original tuple : " + str (test_tup)) # Remove nested records # using isinstance() + enumerate() + loop res = tuple () for count, ele in enumerate (test_tup): if not isinstance (ele, tuple ): res = res + (ele, ) # printing result print ("Elements after removal of nested records : " + str (res)) |
The original tuple : (1, 5, 7, (4, 6), 10) Elements after removal of nested records : (1, 5, 7, 10)
Method 2 : Using type() method
Python3
# Python3 code to demonstrate working of # Remove nested records # initialize tuple test_tup = ( 1 , 5 , 7 , ( 4 , 6 ), 10 ) # printing original tuple print ( "The original tuple : " + str (test_tup)) # Remove nested records res = [] for i in test_tup: if not type (i) is tuple : res.append(i) res = tuple (res) # printing result print ( "Elements after removal of nested records : " + str (res)) |
The original tuple : (1, 5, 7, (4, 6), 10) Elements after removal of nested records : (1, 5, 7, 10)
Method 3 : Using filter()+lambda functions
Python3
# Python3 code to demonstrate working of # Remove nested records # initialize tuple test_tup = ( 1 , 5 , 7 , ( 4 , 6 ), 10 ) # printing original tuple print ( "The original tuple : " + str (test_tup)) # Remove nested records res = list ( filter ( lambda x: not isinstance (x, tuple ), test_tup)) # printing result print ( "Elements after removal of nested records : " + str (res)) |
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
# Python3 code to demonstrate working of # Remove nested records # initialize tuple test_tup = ( 1 , 5 , 7 , ( 4 , 6 ), 10 ) # printing original tuple print ( "The original tuple : " + str (test_tup)) # Remove nested records res = [x for x in test_tup if not isinstance (x, tuple )] # printing result print ( "Elements after removal of nested records : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
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)
Please Login to comment...