![]() |
VOOZH | about |
Sometimes, while working with data, we can have a problem in which we need to pair each element in container to a specific index element, like rear element. This kind of problem can have application in many domains. Lets discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension This is one way in which this task can be performed. In this, we iterate through each row element in list and pair it with rear element using negative indexing of list.
The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]] The list after pairing is : [[[4, 6], [5, 6]], [[2, 5], [4, 5]], [[6, 5], [7, 5]]]
Method #2 : Using product() + loop The combination of above methods can also be used to perform this task. In this, we iterate through the list and perform task of pairing using product and hence reducing one pair of loop.
The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]] The list after pairing is : [[[4, 6], [5, 6]], [[2, 5], [4, 5]], [[6, 5], [7, 5]]]
Method#3: Using zip()+loop.
The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]] The list after pairing is : [[(4, 6), (5, 6)], [(2, 5), (4, 5)], [(6, 5), (7, 5)]]
Time Complexity: O(n^2)
Auxiliary Space: O(n)
Method#4: Using map() and zip_longest()
The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]] The list after pairing is : [[(4, 6), (5, 6)], [(2, 5), (4, 5)], [(6, 5), (7, 5)]]
Time Complexity: O(n^2)
Auxiliary Space: O(n)
Explanation:
Here, we are using map() function to iterate through each element of each sublist. Inside the map function, we use the lambda function to pair each element of the sublist with the last element of the same sublist. We use the zip_longest() function from the itertools module to get the elements of the sublist. We pass the sublist as the first argument, an empty list as the second argument, and the fillvalue as the last element of the sublist. This way we pair each element of the sublist with the last element. Finally, we store all the paired elements of all the sublists inside a list named "res".
Method 5: Using nested loop
Step-by-step approach:
Below is the implementation of the above approach:
The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]] The list after pairing is : [[[4, 6], [5, 6]], [[2, 5], [4, 5]], [[6, 5], [7, 5]]]
Time complexity: O(n^2), where n is the length of the longest sub-list.
Auxiliary space: O(n^2) as we are creating a new list for each sub-list in test_list.
Method #7: Using list comprehension with tuple packing and unpacking:
This method uses list comprehension to pair each element of the row with the last element of the row using tuple packing and unpacking.
Steps:
The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]] The list after pairing is : [[(4, 6), (5, 6)], [(2, 5), (4, 5)], [(6, 5), (7, 5)]]
Time complexity: O(N*M), where N is the number of rows and M is the number of columns in the input matrix.
Auxiliary space: O(N*M), where N is the number of rows and M is the number of columns in the input matrix.