![]() |
VOOZH | about |
Sometimes, we might have to perform certain problems related to tuples in which we need to segregate the tuple elements to combine with each element of complex tuple element( such as list ). This can have application in situations we need to combine values to form a whole. Let's discuss certain ways in which this can be performed.
Method #1: Using list comprehension We can solve this particular problem using the list comprehension technique in which we can iterate for each tuple list and join it with other tuple attributes to join.
Step-by-step approach:
Below is the implementation of the above approach:
The original list : [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')] The list tuple combination : [(1, 'gfg'), (2, 'gfg'), (3, 'gfg'), (5, 'cs'), (4, 'cs'), (3, 'cs')]
Time Complexity: O(n^2)
Auxiliary Space: O(n)
Method #2: Using product() + list comprehension Apart from using the tuple for generation of tuples, the product function can be used to get Cartesian product of list elements with tuple element, using the iterator internally.
The original list : [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')] The list tuple combination : [(1, 'gfg'), (2, 'gfg'), (3, 'gfg'), (5, 'cs'), (4, 'cs'), (3, 'cs')]
Time complexity: O(n*m), where n is the number of tuples in the list and m is the maximum length of the tuples in the list.
Auxiliary space: O(n*m), as we are creating a new list of tuples with all possible combinations of the original tuples.
Method #3: Using enumerate function
[(1, 'gfg'), (2, 'gfg'), (3, 'gfg'), (5, 'cs'), (4, 'cs'), (3, 'cs')]
Time complexity: O(n^2) where n is the length of the longest list inside the tuples in test_list.
Auxiliary space: O(n^2) where n is the length of the longest list inside the tuples in test_list.
Method #4: Using for loop
The original list : [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')] The list tuple combination : [(1, 'gfg'), (2, 'gfg'), (3, 'gfg'), (5, 'cs'), (4, 'cs'), (3, 'cs')]
Time Complexity: O(M*N)
Auxiliary Space: O(1)
Method #5 : Using chain.from_iterable() from the itertools module:
The original list : [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')] The list tuple combination : [(1, 'gfg'), (2, 'gfg'), (3, 'gfg'), (5, 'cs'), (4, 'cs'), (3, 'cs')]
Time complexity: O(n) where n is number of elements in all tuples because it iterates through the elements in the list of tuples once.
Auxiliary space: O(n) because it creates a new list to store the resulting tuples.
Method #6: Using reduce() function from functools module + list comprehension.
The original list : [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')] The list tuple combination : [(1, 'gfg'), (2, 'gfg'), (3, 'gfg'), (5, 'cs'), (4, 'cs'), (3, 'cs')]
Time complexity: O(n * m), where n is the length of the input list and m is the maximum length of a tuple in the input list.
Auxiliary space: O(n * m), where n is the length of the input list and m is the maximum length of a tuple in the input list.
Method #7: Using nested loops
The original list : [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')] The list tuple combination : [(1, 'gfg'), (2, 'gfg'), (3, 'gfg'), (5, 'cs'), (4, 'cs'), (3, 'cs')]
Time complexity: O(n * m), where n is the length of the input list and m is the maximum length of a tuple in the input list.
Auxiliary space: O(n * m), where n is the length of the input list and m is the maximum length of a tuple in the input list.