Python | Unpacking nested tuples
Last Updated :
03 Apr, 2023
Sometimes, while working with Python list of tuples, we can have a problem in which we require to unpack the packed tuples. This can have a possible application in web development. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension This task can be performed using list comprehension in which we iterate for tuples and construct the desired tuple. This technique is useful in case we know the exact number of tuple elements and positioning.
Python3
test_list = [( 4 , ( 5 , 'Gfg' )), ( 7 , ( 8 , 6 ))]
print ("The original list is : " + str (test_list))
res = [(x, y, z) for x, (y, z) in test_list]
print ("The unpacked nested tuple list is : " + str (res))
|
Output :
The original list is : [(4, (5, 'Gfg')), (7, (8, 6))]
The unpacked nested tuple list is : [(4, 5, 'Gfg'), (7, 8, 6)]
Time complexity: O(n), where n is the size of the input list “test_list”.
Auxiliary space: O(n), where n is the size of the input list “test_list”. This is because the program creates a new list “res” of the same size as “test_list” to store the unpacked tuples.
Method #2 : Using list comprehension + “*” operator Many times, there might be a case in which we don’t know the exact number of element in tuple and also the element count is variable among tuples. The “*” operator can perform the task of this variable unpacking.
Python3
test_list = [( 4 , ( 5 , 'Gfg' )), ( 7 , ( 8 , 6 ))]
print ("The original list is : " + str (test_list))
res = [(z, * x) for z, x in test_list]
print ("The unpacked nested tuple list is : " + str (res))
|
Output :
The original list is : [(4, (5, 'Gfg')), (7, (8, 6))]
The unpacked nested tuple list is : [(4, 5, 'Gfg'), (7, 8, 6)]
The time complexity of this code is O(n), where n is the number of tuples in the input list.
The auxiliary space complexity is also O(n), where n is the number of tuples in the input list.
Method #3: Using nested loops
- Initialize an empty list ‘res’ to store the unpacked tuples
- Iterate through the tuples in the original list using a for loop
- Initialize a temporary list to store the unpacked tuple:
- Iterate through the elements of the nested tuple using another for loop:
- Append each element of the nested tuple to the temporary list:
- Append the temporary list to the result list:
- Print the unpacked nested tuple list:
Python3
test_list = [( 4 , ( 5 , 'Gfg' )), ( 7 , ( 8 , 6 ))]
print ( "The original list is : " + str (test_list))
res = []
for z, x in test_list:
temp = [z]
for item in x:
temp.append(item)
res.append(temp)
print ( "The unpacked nested tuple list is : " + str (res))
|
Output
The original list is : [(4, (5, 'Gfg')), (7, (8, 6))]
The unpacked nested tuple list is : [[4, 5, 'Gfg'], [7, 8, 6]]
Time complexity: O(n*m), where n is the number of tuples in the original list and m is the maximum number of elements in a nested tuple.
Auxiliary space: O(n*m), because we are creating a temporary list for each nested tuple and appending it to the result list.
Method #4 : Using extend(),type(),tuple() methods
Approach
- Initiate a for loop to traverse list of nested tuples
- Initiate another for loop inside above for loop traverse each nested tuple
- In the nested tuple if the type is tuple append tuple elements to list and other elements to list
- Finally convert list to tuple and append it as part of output list
- Display output list
Python3
test_list = [( 4 , ( 5 , 'Gfg' )), ( 7 , ( 8 , 6 ))]
print ( "The original list is : " + str (test_list))
res = []
for i in test_list:
x = []
for j in i:
if type (j) is tuple :
x.extend( list (j))
else :
x.append(j)
res.append( tuple (x))
print ( "The unpacked nested tuple list is : " + str (res))
|
Output
The original list is : [(4, (5, 'Gfg')), (7, (8, 6))]
The unpacked nested tuple list is : [(4, 5, 'Gfg'), (7, 8, 6)]
Time complexity: O(n*m), where n is the number of tuples in the original list and m is the maximum number of elements in a nested tuple.
Auxiliary space: O(n*m), because we are creating a temporary list for each nested tuple and appending it to the result list.
Method #6: Using generator expressions
Unpack the nested tuples using generator expressions. This approach is similar to using list comprehension, but it generates elements on the fly rather than creating a new list.
Step-by-step approach:
- Create a list of nested tuples test_list.
- Print the original list.
- Use a generator expression to unpack the nested tuples.
- The generator expression (x, y, z) for x, (y, z) in test_list iterates over each tuple in test_list, and unpacks the second element of the tuple using (y, z). It then yields a new tuple with three elements (x, y, z).
- Convert the generator expression to a list using the list() function and assign the result to res.
- Print the unpacked nested tuple list res.
Below is the implementation of the above approach:
Python3
test_list = [( 4 , ( 5 , 'Gfg' )), ( 7 , ( 8 , 6 ))]
print ( "The original list is : " + str (test_list))
res = ((x, y, z) for x, (y, z) in test_list)
print ( "The unpacked nested tuple list is : " + str ( list (res)))
|
Output
The original list is : [(4, (5, 'Gfg')), (7, (8, 6))]
The unpacked nested tuple list is : [(4, 5, 'Gfg'), (7, 8, 6)]
Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(1), as we are not creating a new list or using any additional memory to store the generator expression.
Please Login to comment...