Open In App

Python | Unpacking nested tuples

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 code to demonstrate working of
# Unpacking nested tuples
# using list comprehension
 
# initialize list
test_list = [(4, (5, 'Gfg')), (7, (8, 6))]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Unpacking nested tuples
# using list comprehension
res = [(x, y, z) for x, (y, z) in test_list]
 
# printing result
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 code to demonstrate working of
# Unpacking nested tuples
# using list comprehension + * operator
 
# initialize list
test_list = [(4, (5, 'Gfg')), (7, (8, 6))]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Unpacking nested tuples
# using list comprehension + * operator
res = [(z, *x) for z, x in test_list]
 
# printing result
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 list
test_list = [(4, (5, 'Gfg')), (7, (8, 6))]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Unpacking nested tuples using nested loops
res = []
for z, x in test_list:
    temp = [z]
    for item in x:
        temp.append(item)
    res.append(temp)
 
# printing result
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

  1. Initiate a for loop to traverse list of nested tuples
  2. Initiate another for loop inside above for loop traverse each nested tuple
  3. In the nested tuple if the type is tuple append tuple elements to list and other elements to list
  4. Finally convert list to tuple and append it as part of output list
  5. Display output list




# Python3 code to demonstrate working of
# Unpacking nested tuples
 
# initialize list
test_list = [(4, (5, 'Gfg')), (7, (8, 6))]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Unpacking nested tuples
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))
         
# printing result
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:

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Unpacking nested tuples
# using generator expressions
 
# initialize list
test_list = [(4, (5, 'Gfg')), (7, (8, 6))]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Unpacking nested tuples
# using generator expressions
res = ((x, y, z) for x, (y, z) in test_list)
 
# printing result
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.


Article Tags :