Open In App

Python | Unpacking nested tuples

Last Updated : 03 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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




# 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 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




# 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




# 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:

  • 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




# 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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads