Python – Find the Maximum of Similar Indices in two list of Tuples
Last Updated :
13 Apr, 2023
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
test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )]
test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )]
print ("The original list 1 : " + str (test_list1))
print ("The original list 2 : " + str (test_list2))
res = [( max (x[ 0 ], y[ 0 ]), max (x[ 1 ], y[ 1 ]))
for x, y in zip (test_list1, test_list2)]
print ("The Maximum across lists is : " + str (res))
|
Output :
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
test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )]
test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )]
print ("The original list 1 : " + str (test_list1))
print ("The original list 2 : " + str (test_list2))
res = [ tuple ( map ( max , zip (a, b)))
for a, b in zip (test_list1, test_list2)]
print ("The Maximum across lists is : " + str (res))
|
Output :
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
test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )]
test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
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])
print ( "The Maximum across lists is : " + str (res))
|
Output
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
test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )]
test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )]
array1 = np.array(test_list1)
array2 = np.array(test_list2)
res = np.maximum(array1, array2)
print ( "The Maximum across lists is : " + str (res))
|
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
test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )]
test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
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 ])))
print ( "The Maximum across lists is : " + str (res))
|
Output
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
def max_similar_indices(test_list1, test_list2):
if not test_list1 or not test_list2:
return []
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 :])
test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )]
test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = max_similar_indices(test_list1,test_list2)
print ( "The Maximum across lists is : " + str (res))
|
Output
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.
Similar Reads
Python - Maximum of Similar Keys in Tuples
Sometimes, while working with Python tuples, we can have a problem in which we need to perform maximum of all values of the equal keys in Tuple list. This kind of application in useful in many domains such as web development and day-day programming. Let's discuss certain ways in which this task can
4 min read
Python - Similar index pairs in Tuple lists
Sometimes, while working with Python tuples, we can have a problem in which we need to perform similar index pairing. This kind of problem is peculiar, but can occur across certain domains. Let's discuss certain way in which this task can be performed. Input : test_list1 = [(5, ), (1, ), (8, ), (10,
5 min read
Python - Find index of maximum item in list
Given a list of N integers, the task is to write a Python program to find the index of the maximum element in the Python list. Example: Input: [ 2, 4, 6, 1, 8, 5, 3 ]Output: Index of the max element in a list is 4Explanation: The max element is 8 and its position is 4. Input: [ 10, 1, 5, 3, 8, 9, 7
6 min read
Python | Find mismatch item on same index in two list
Given two list of integers, the task is to find the index at which the element of two list doesn't match. Input: Input1 = [1, 2, 3, 4] Input2 = [1, 5, 3, 6] Output: [1, 3] Explanation: At index=1 we have 2 and 5 and at index=3 we have 4 and 6 which mismatches. Below are some ways to achieve this tas
4 min read
Python | Tuples with maximum key of similar values
Sometimes, while working with Python, we can have a problem in which we need to get all the records. This data can have similar values and we need to find maximum key-value pair. This kind of problem can occur while working with data. Let's discuss certain ways in which this task can be done. Method
6 min read
Python - Records Maxima in List of Tuples
Sometimes, while working with records, we can have a problem in which we need to the maximum all the columns of a container of lists that are tuples. This kind of application is common in the web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using ma
5 min read
Python - K Maximum elements with Index in List
GIven a List, extract K Maximum elements with their indices. Input : test_list = [5, 3, 1, 4, 7, 8, 2], K = 2 Output : [(4, 7), (5, 8)] Explanation : 8 is maximum on index 5, 7 on 4th. Input : test_list = [5, 3, 1, 4, 7, 10, 2], K = 1 Output : [(5, 10)] Explanation : 10 is maximum on index 5. Method
4 min read
Python - Join Tuples if similar initial element
Sometimes, while working with Python tuples, we can have a problem in which we need to perform concatenation of records from the similarity of initial element. This problem can have applications in data domains such as Data Science. Let's discuss certain ways in which this task can be performed. Inp
8 min read
Python | Get first element with maximum value in list of tuples
In Python, we can bind structural information in form of tuples and then can retrieve the same. But sometimes we require the information of tuple corresponding to maximum value of other tuple indexes. This functionality has many applications such as ranking. Let's discuss certain ways in which this
4 min read
Python program to sort a dictionary list based on the maximum value
Given list with dictionaries as elements, write a Python program to sort the dictionary on the basis maximum value in a key value pair of a dictionary. In simpler terms, first each element of a list (which is a dictionary) will be checked, meaning each key value pair will be compared to find the ele
5 min read