Open In App

Python | Index Maximum among Tuples

Last Updated : 22 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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




# 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




# 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()

  • Import numpy module as np.
  • Initialize tuples test_tup1 and test_tup2.
  • Calculate the maximum of each corresponding index of tuples using np.maximum().
  • Convert the resultant array to tuple.
  • Return the resultant tuple.

Python3




#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.

  • Initialize the two tuples test_tup1 and test_tup2.
  • Create a list comprehension that zips the two tuples and finds the maximum of each pair.
  • Convert the list comprehension to a tuple using the tuple() method.
  • Assign the resultant tuple to the variable res.
  • Print the resultant tuple res.

Python3




# 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

  • Import the starmap() function from the itertools module.
  • Then we initialize the two tuples test_tup1 and test_tup2.
  • After that, we use the zip() function to combine the two tuples element-wise and pass the resulting iterator to the starmap() function.
  • The starmap() function applies the max() function on each pair of elements from the two tuples and returns an iterator over the resulting values.
  • Finally, we convert the resulting iterator to a tuple using the tuple() function and print the result.

Python3




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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads