![]() |
VOOZH | about |
Sometimes we need to convert between the data types, primarily due to the reason of feeding them to some function or output. This article solves a very particular problem of pairing like indices in list of lists and then construction of list of tuples of those pairs. Let's discuss how to achieve the solution of this problem.
Method #1 : Using zip() + list comprehension We can use zip function to link to each element's like indices and list comprehension can be used to perform the logic behind the conversion to tuple and extending the logic to all the sublists.
The original list 1 : [[1, 4, 5], [8, 7], [2]] The original list 2 : [['g', 'f', 'g'], ['f', 'r'], ['u']] The paired list of tuple is : [(1, 'g'), (4, 'f'), (5, 'g'), (8, 'f'), (7, 'r'), (2, 'u')]
Time complexity: O(n), where n is the sum of the lengths of test_list1 and test_list2.
Auxiliary space : O(n), where n is the length of the result list.
Method #2 : Using zip() + itertools.chain.from_iterable() This problem can also be solved using the zip function along with the from_iterable function. The task performed by zip function remains the same but conversion to tuple and do it for all elements can be handled by from_iterable function.
The original list 1 : [[1, 4, 5], [8, 7], [2]] The original list 2 : [['g', 'f', 'g'], ['f', 'r'], ['u']] The paired list of tuple is : [(1, 'g'), (4, 'f'), (5, 'g'), (8, 'f'), (7, 'r'), (2, 'u')]
Time complexity: O(n), where n is the sum of the lengths of all sublists in the input lists test_list1 and test_list2.
Auxiliary space: O(n)
Method #3 : Using extend() and for loops
The original list 1 : [[1, 4, 5], [8, 7], [2]] The original list 2 : [['g', 'f', 'g'], ['f', 'r'], ['u']] The paired list of tuple is : [(1, 'g'), (4, 'f'), (5, 'g'), (8, 'f'), (7, 'r'), (2, 'u')]
Time Complexity : O(N*N)
Auxiliary Space : O(N)
Method#4: Using Recursive method.
Algorithm:
The original list 1 : [[1, 4, 5], [8, 7], [2]] The original list 2 : [['g', 'f', 'g'], ['f', 'r'], ['u']] The paired list of tuple is : [(1, 'g'), (4, 'f'), (5, 'g'), (8, 'f'), (7, 'r'), (2, 'u')]
Time Complexity : O(N*M), where N is the number of sublists in test_list1 and test_list2 and M is the maximum length of any sublist in both lists. This is because the function needs to iterate through all elements of the sublists in both lists and create a tuple for each element.
Auxiliary Space : O(N*M), as the function creates a new list res for each recursion level and the total number of tuples in the resulting list is n x m.
Method #5: Using a nested for loop.
Step-by-step approach:
Below is the implementation of the above approach:
The original list 1 : [[1, 4, 5], [8, 7], [2]] The original list 2 : [['g', 'f', 'g'], ['f', 'r'], ['u']] The paired list of tuple is : [(1, 'g'), (4, 'f'), (5, 'g'), (8, 'f'), (7, 'r'), (2, 'u')]
Time complexity: O(n^2), where n is the length of the longest sublist, since we have to iterate through each element of each sublist.
Auxiliary space: O(n^2), since we need to store all the paired tuples in a list.
Method 6: Using map() with lambda function
Step-by-step explanation:
The paired list of tuple is : [(1, 'g'), (4, 'f'), (5, 'g'), (8, 'f'), (7, 'r'), (2, 'u')]
Time complexity: O(n*m), where n is the length of the longest sub-list in test_list1 and test_list2, and m is the number of sub-lists in test_list1 and test_list2.
Auxiliary space: O(n*m), to store the resulting paired list of tuples.
Method 7: Using reduce():
Algorithm:
Below is the implementation of the above approach:
The original list 1 : [[1, 4, 5], [8, 7], [2]] The original list 2 : [['g', 'f', 'g'], ['f', 'r'], ['u']] The paired list of tuple is : [((1, 'g'), (4, 'f')), ((5, 'g'), (8, 'f')), ((7, 'r'), (2, 'u'))]
Time Complexity: The time complexity of the given code is O(n), where n is the total number of elements in the nested lists. The reduce() function in the code runs in linear time complexity.
Space Complexity: The space complexity of the code is O(n), where n is the total number of elements in the nested lists. This is because we need to store all the elements of the flattened lists and the paired tuples in memory.
Method 8: Using heapq:
Algorithm:
The original list 1 : [[1, 4, 5], [8, 7], [2]] The original list 2 : [['g', 'f', 'g'], ['f', 'r'], ['u']] The paired list of tuple is : [(1, 'g'), (2, 'u'), (4, 'f'), (5, 'g'), (8, 'f'), (7, 'r')]
Time complexity:
The time complexity of this code is O(n log n) due to the use of heapq.merge() function which has a time complexity of O(n log n).
Space complexity:
The space complexity of this code is O(n) due to the creation of a new list to store the merged and paired tuples.