Python Program to find the largest element in a tuple


One of the most common problems in computer science is the problem of searching. To check whether a given element exists in a variable is important and sometimes the item we must search for, can be the maximum, minimum, most frequent etc. in this article we will see how we can find the largest element in a tuple.

We know that tuple is a pre-defined datatype that stores heterogenous data. It is sort of a container that holds several items inside it.

We can define a tuple in python using the round brackets enclosing the data that we wish to store −

Var = (1, ‘a’, 3.7)

We can find the maximum element of a tuple in different ways. We will discuss the naïve way as well as the pythonic way to search for the maximum element.

Using For Loop

As a tuple is basically an iterable we can use the iterative statements of python to go through all the elements of the tuple one by one. That way we can determine the largest by comparing all the values.

We will use a variable to hold the greatest value at a given point in time. When the loop has gone over all the elements the variable will hold the largest element. Let us look at the algorithm for the program.

Algorithm

Lets look at the algorithm for the above method −

Step 1 - Create a tuple

Step 2 – Create a variable to store the max element and store the first value of the tuple in it.

Step 3 – Start iterating over the elements using for loop from the second element onwards

Step 4 – Check whether the current value is greater than the max variable

Step 5 – if yes, then update the max variable and set its value to the current variable

Step 6 – if no, then continue to the next element

Step 7 – When the program gets out of the loop, print the max element

Example

A = (1, 2, 3)
maxElem = A[0]
for element in A:
   if element > maxElem:
      maxElem = element
print("Maximum Element in the tuple is : ", maxElem )

Output

Maximum Element in the tuple is : 3

Explanation

In this method we first take the 1st element as the maximum element then we iterate over the list and compare the current element with the current maximum element and if the current element is greater than the current maximum element we update the current maximum element. After the iteration we will print the maximum element.

Using the sorted() method

The sorted() function returns a sorted list of the specified iterable object.

We can always choose from the vast set of libraries and built-in methods to easily solve most of the common problems in python.

This approach discusses the use of the built-in python function sorted(). This takes in an iterable as a parameter and returns a sorted list of its elements.

We will use this function to sort the elements of the tuple in ascending order and access the last element from the resulting list. That last element will be the maximum element of the tuple.

Algorithm

Step 1 – Create a tuple

Step 2 - Use the sorted function with the tuple as an argument

Step 3 – Access the last element using negative indexing and store it in a new variable

Step 4 – Print the newly created variable

Example

Following example sorts the array using the sorted() method. In this example we are trying to sort the whole list and then print the last element of this sorted array to get the largest element.

inputArray = (5, 2, 2, 1, 7)
sortedArray = sorted(inputArray)
print("Maximum Element is: ", inputArray[-1])

Output

Maximum Element is: 7

Using the max() method

Python also provides a method named max() to find the greatest element from the list, tuple, etc.

As we discussed in the above approach there are several useful methods that are built-in python and can be used to solve mostly all the common problems.

It takes in an iterable, or two or more number type objects as input argument and returns the maximum out of all the elements in the iterable.

Syntax 

The syntax for using the max method is as follows.

max(iterable, *, key=None)
max(iterable, *, default, key=None)
max(arg1, arg2, *args, key=None)

Where, a and b are values from which you need to find the maximum element.

Algorithm

Step 1 – Create a tuple

Step 2 – Create a variable to store the maximum element

Step 3 – Use the max method with the tuple as input argument

Step 4 – print the maximum element

Example

In this example we used the max function of python to get the maximum element of the whole list at once.

array = (1, 5, 2, 8, 7, 0)
maxElement = max(array)
print("Maximum Element is : ", maxElement)

Output

Maximum Element is : 8

Conclusion

In this article, we focused on three different ways to find the maximum element in a tuple. We saw how we can use the naïve approach of using the for loop to implement linear search.

We also saw how we can make use of methods like sorted and max to find the element. The use of built-in methods is preferred as they are already optimized to perform in a fast and efficient manner.

Updated on: 20-Feb-2023

671 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements