Example:
tuple = ("python", "includehelp", 43, 54.23)
Index maximum among tuples
In this program, we are given two tuples with integer values as elements of the same size. We will be creating a python program that will create a tuple with all elements from the tuples, each index element will be the element with maximum value out of both tuples at the given index.
Input:
tup1 = (2, 3, 6, 1); tup2 = (9, 1, 4, 7)
Output:
(9, 3, 6, 7)
To index maximum among tuples, we will be traversing both the tuple and then comparing value at each index and feeding the maximum value out of both to the max tuple. For performing this series of tasks in python, we have multiple method combinations which can be used based on the requirement of the program. Let's see some of the methods in working.
Method 1:
One method to index maximum among tuples is by mapping elements of both tuples where the mapping function is a lambda function which task elements from both tuples and exacts the maximum of them using the max() method. And this map is then converted to a tuple using the tuple() method.
Output:
Method 2:
Another method to solve the problem is by mapping the maximum value using max() method for the indexes of both tuples zipped together using zip() method.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer