# Python | Index Maximum among Tuples

In this program, we are given two tuples consisting of integer elements. We need to create a Python program to index the maximum values element from each tuple.
Submitted by Shivang Yadav, on November 07, 2021

Tuples in python are of great importance and are used a lot to store and manipulate data in python. Comparision and extraction based on it are common in data science and related field and Python's ability to perform such tasks with collections effortlessly makes it a suitable programming language for advanced tasks like AI, data science, etc. In this article, we will learn one such manipulation technique. Here, we will take two tuples with integer values and return the element at each index which is maximum amongst the elements of both the tuples at the given index.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

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.

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

Preparation

What's New

Top Interview Coding Problems/Challenges!