Open In App

Python | Index Maximum among Tuples

Sometimes, while working with records, we might have a common problem of maximizing contents of one tuple with corresponding index of other tuple. This has application in almost all the domains in which we work with tuple records. Let’s discuss certain ways in which this task can be performed. 

Method #1: Using map() + lambda + max()
A combination of the above functionalities can solve the problem for us. In this, we compute the maximum using lambda functions and max() and extend the logic to keys using map(). 






# Python3 code to demonstrate working of
# Index Maximum among Tuples
# using map() + lambda + max()
 
# initialize tuples
test_tup1 = (10, 4, 5)
test_tup2 = (2, 5, 18)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Index Maximum among Tuples
# using map() + lambda + max()
res = tuple(map(lambda i, j: max(i, j), test_tup1, test_tup2))
 
# printing result
print("Resultant tuple after maximization : " + str(res))

Output : 
The original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after maximization : (10, 5, 18)

Method #2: Using map() + zip() + max() 



The combination of above functions can also be used to achieve this particular task. In this, we add inbuilt max() to perform maximization and zip the like indices using zip() and extend logic to both tuples using map(). 




# Python3 code to demonstrate working of
# Index Maximum among Tuples
# using map() + zip() + max()
 
# initialize tuples
test_tup1 = (10, 4, 5)
test_tup2 = (2, 5, 18)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Index Maximum among Tuples
# using map() + zip() + max()
res = tuple(map(max, zip(test_tup1, test_tup2)))
 
# printing result
print("Resultant tuple after maximization : " + str(res))

Output : 
The original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after maximization : (10, 5, 18)

Method #3: Using for loop and max(),tuple() methods




# Python3 code to demonstrate working of
# Index Maximum among Tuples
 
# initialize tuples
test_tup1 = (10, 4, 5)
test_tup2 = (2, 5, 18)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Index Maximum among Tuples
res=[]
for i in range(0,len(test_tup1)):
    res.append(max(test_tup1[i],test_tup2[i]))
res=tuple(res)
# printing result
print("Resultant tuple after maximization : " + str(res))

Output
The original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after maximization : (10, 5, 18)

Method #4: Using numpy()




#Python3 code to demonstrate working of
#Index Maximum among Tuples using numpy
import numpy as np
 
#initialize tuples
test_tup1 = (10, 4, 5)
test_tup2 = (2, 5, 18)
 
#printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
#Index Maximum among Tuples
#using numpy
res = tuple(np.maximum(test_tup1, test_tup2))
#printing result
print("Resultant tuple after maximization : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy

Output:

The original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after maximization : (10, 5, 18)

Time complexity : O(n)
Auxiliary Space : O(n)

Method #5: Using list comprehension + zip() + max()

This program takes two tuples as input and returns a new tuple that contains the maximum values of the corresponding elements of the input tuples. It does this using a list comprehension that zips the two input tuples and finds the maximum of each pair using the max() function. Finally, it converts the resulting list to a tuple using the tuple() function and prints the output.




# Python3 code to demonstrate working of
# Index Maximum among Tuples
# using list comprehension + zip() + max()
 
# initialize tuples
test_tup1 = (10, 4, 5)
test_tup2 = (2, 5, 18)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Index Maximum among Tuples
# using list comprehension + zip() + max()
res = tuple(max(i) for i in zip(test_tup1, test_tup2))
 
# printing result
print("Resultant tuple after maximization : " + str(res))

Output
The original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after maximization : (10, 5, 18)

Time complexity: O(n), where n is the length of the tuples.
Auxiliary space: O(n), where n is the length of the tuples (used to store the resultant tuple).

Method #6: Using the itertools module’s starmap() function and the max() function




from itertools import starmap
 
# initialize tuples
test_tup1 = (10, 4, 5)
test_tup2 = (2, 5, 18)
 
# print original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# find maximum index using starmap() and max()
res = tuple(starmap(max, zip(test_tup1, test_tup2)))
 
# print result
print("Resultant tuple after maximization : " + str(res))

Output
The original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after maximization : (10, 5, 18)

Time Complexity: O(n), where n is the length of the tuples.
Auxiliary Space: O(n), where n is the length of the tuples.


Article Tags :