Q:

Python | Index Maximum among Tuples

belongs to collection: Python Tuple Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

# Python program to index maximum among tuples 
# using map and lambda, 

# Creating and Printing tuples 
tuple1 = (2, 3, 6, 1)
tuple2 = (9, 1, 4, 7)
print("The elements of tuple 1 : " + str(tuple1))
print("The elements of tuple 2 : " + str(tuple2))

# Indexing maximum among Tuples 
# using max and tuple
indexMaxTuple = tuple(map(lambda i, j: max(i, j), tuple1, tuple2))

# Printing result
print("The elements of tuple with maximum index tuples are : " + str(indexMaxTuple))

Output:

The elements of tuple 1 : (2, 3, 6, 1)
The elements of tuple 2 : (9, 1, 4, 7)
The elements of tuple with maximum index tuples are : (9, 3, 6, 7)

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.

# Python program to index maximum among tuples 
# using map and lambda, 

# Creating and Printing tuples 
tuple1 = (2, 3, 6, 1)
tuple2 = (9, 1, 4, 7)
print("The elements of tuple 1 : " + str(tuple1))
print("The elements of tuple 2 : " + str(tuple2))

# Indexing maximum among Tuples using max and tuple
indexMaxTuple = tuple(map(max, zip(tuple1, tuple2)))

# Printing result
print("The elements of tuple with maximum index tuples are : " + str(indexMaxTuple))

Output:

The elements of tuple 1 : (2, 3, 6, 1)
The elements of tuple 2 : (9, 1, 4, 7)
The elements of tuple with maximum index tuples are : (9, 3, 6, 7)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Nested Tuples Subtraction... >>
<< Python | Rear elements from Tuple Strings...