Sometimes, while working with Python records, we can have a problem in which we need to perform cross maximization of list of tuples. This kind of application is popular in web development domain. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + zip() The combination of above functionalities can be used to perform this particular task. In this, we iterate through the list using list comprehension and the maximization across lists is performed with help of zip().
Python3
# Python3 code to demonstrate working of # Maximum of Tuple List Similar Indices # using list comprehension + zip() # initialize lists test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )] test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )] # printing original lists print ("The original list 1 : " + str (test_list1)) print ("The original list 2 : " + str (test_list2)) # Maximum of Tuple List Similar Indices # using list comprehension + zip() res = [( max (x[ 0 ], y[ 0 ]), max (x[ 1 ], y[ 1 ])) for x, y in zip (test_list1, test_list2)] # printing result print ("The Maximum across lists is : " + str (res)) |
The original list 1 : [(2, 4), (6, 7), (5, 1)] The original list 2 : [(5, 4), (8, 10), (8, 14)] The Maximum across lists is : [(5, 4), (8, 10), (8, 14)]
Method #2 : Using max() + zip() + map() This is yet another way to perform this task. This is similar to the above method, the difference is that maximization is performed by an inbuilt function, and extending logic to each element is done by map().
Python3
# Python3 code to demonstrate working of # Maximum of Tuple List Similar Indices # using max() + zip() + map() # initialize lists test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )] test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )] # printing original lists print ("The original list 1 : " + str (test_list1)) print ("The original list 2 : " + str (test_list2)) # Maximum of Tuple List Similar Indices # using max() + zip() + map() res = [ tuple ( map ( max , zip (a, b))) for a, b in zip (test_list1, test_list2)] # printing result print ("The Maximum across lists is : " + str (res)) |
The original list 1 : [(2, 4), (6, 7), (5, 1)] The original list 2 : [(5, 4), (8, 10), (8, 14)] The Maximum across lists is : [(5, 4), (8, 10), (8, 14)]
Time complexity: The code uses nested loops, but both loops iterate over lists of the same length, so the time complexity is O(n), where n is the length of the input lists.
Auxiliary space: The code creates a new list, “res”, to store the result, and creates two tuples and a list object in the list comprehension. Therefore, the space complexity is O(n), where n is the length of the input lists.
Method #3: Using sum(),list() methods
Python3
# Python3 code to demonstrate working of # Maximum of Tuple List Similar Indices # initialize lists test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )] test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )] # printing original lists print ( "The original list 1 : " + str (test_list1)) print ( "The original list 2 : " + str (test_list2)) # Maximum of Tuple List Similar Indices res = [] for i in range ( 0 , len (test_list1)): if ( sum ( list (test_list1[i]))> sum ( list (test_list2[i]))): res.append(test_list1[i]) else : res.append(test_list2[i]) # printing result print ( "The Maximum across lists is : " + str (res)) |
The original list 1 : [(2, 4), (6, 7), (5, 1)] The original list 2 : [(5, 4), (8, 10), (8, 14)] The Maximum across lists is : [(5, 4), (8, 10), (8, 14)]
Time complexity: O(n), where n is the length of the input lists.
Auxiliary space: O(n), where n is the length of the input lists.
Method #4: Using numpy()
Note: Install numpy module using command “pip install numpy”
Another approach to find the maximum of similar indices in two list of tuples is using the numpy library. The numpy library provides an efficient way to perform mathematical operations on arrays and matrices. One of the useful functions in numpy is the maximum() function, which can be used to find the maximum value of an array.
Here’s an example of how to use numpy to find the maximum of similar indices in two list of tuples:
Python3
import numpy as np # initialize lists test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )] test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )] # convert lists to numpy arrays array1 = np.array(test_list1) array2 = np.array(test_list2) # find the maximum of similar indices res = np.maximum(array1, array2) # printing result print ( "The Maximum across lists is : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
This approach has a time complexity of O(n) and an auxiliary space of O(n) where n is the size of the two lists. It is more efficient than the previous approaches because numpy performs the operations on the arrays at a low-level using C/C++ making it faster.
Method 5: Using for loop
One such approach would be to use a for loop to iterate through both lists and compare each tuple, taking the maximum value of each tuple and creating a new list with the results.
Python3
# initialize lists test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )] test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )] # printing original lists print ( "The original list 1 : " + str (test_list1)) print ( "The original list 2 : " + str (test_list2)) # Maximum of Tuple List Similar Indices res = [] for i in range ( len (test_list1)): x = test_list1[i] y = test_list2[i] res.append(( max (x[ 0 ], y[ 0 ]), max (x[ 1 ], y[ 1 ]))) # printing result print ( "The Maximum across lists is : " + str (res)) |
The original list 1 : [(2, 4), (6, 7), (5, 1)] The original list 2 : [(5, 4), (8, 10), (8, 14)] The Maximum across lists is : [(5, 4), (8, 10), (8, 14)]
Time complexity: O(n), where n is the number of tuples in the list. This is because the loop iterates through the two lists once and performs a constant amount of work for each iteration.
Auxiliary space: O(n), as a new list res is created with the result of the maximum of each tuple.
Method 6: Using Recursive method.
The algorithm to find the maximum of tuple list similar indices using recursion is as follows:
- Define a function max_similar_indices that takes two tuple lists as arguments.
- If either of the input lists is empty, return an empty list.
- Otherwise, obtain the first tuple from each list and find the maximum value of each corresponding element.
- Combine these maximum values into a tuple and append it to the result list.
- Recursively call the function with the remaining tuples in both input lists and append the results to the current result list.
- Return the result list.
Python3
# Python3 code to demonstrate working of # Maximum of Tuple List Similar Indices def max_similar_indices(test_list1, test_list2): # base case: if lists are empty, return empty list if not test_list1 or not test_list2: return [] # recursive case: find maximum of first tuple in both lists, # and combine with the result of the recursion on the remaining tuples else : x, y = test_list1[ 0 ], test_list2[ 0 ] return [( max (x[ 0 ], y[ 0 ]), max (x[ 1 ], y[ 1 ]))] + \ max_similar_indices(test_list1[ 1 :], test_list2[ 1 :]) # initialize lists test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )] test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )] # printing original lists print ( "The original list 1 : " + str (test_list1)) print ( "The original list 2 : " + str (test_list2)) # Maximum of Tuple List Similar Indices res = max_similar_indices(test_list1,test_list2) # printing result print ( "The Maximum across lists is : " + str (res)) |
The original list 1 : [(2, 4), (6, 7), (5, 1)] The original list 2 : [(5, 4), (8, 10), (8, 14)] The Maximum across lists is : [(5, 4), (8, 10), (8, 14)]
The time complexity of this algorithm is O(n), where n is the length of the input lists, since we need to process each tuple once in the worst case. The space complexity is also O(n), since we need to store the result list, which could have a maximum size of n.