How to find the element from a Python list with a maximum value?


List is one of the most commonly used data structures provided by python. List is a data structure in Python that is mutable and has an ordered sequence of elements. Following is a list of integer values

Example

Following is a list of integer values.

lis= [1,2,3,4,5]
print(lis)

Output

If you execute the above snippet, it produces the following output.

[1, 2, 3, 4, 5]

In this article, we will look at different ways to find the maximum of the list in Python. For the above list, the maximum element of the list is 5.

Using the max() function

In this method, we use the max() function to find the maximum valued element. The max() function returns an element from the list with the maximum value.

Example

The following is an example code for finding the maximum element of a list using the max() function.

list1 = [1, 0, 4, 5, 100] print("Largest element of the list is:", max(list1))

Output

If you execute the above snippet, it produces the following output.

Largest element of the list is: 100

Using the sort() method

Here we will find the maximum element of a list using the sort() method. The sort() method which is provided by python is used to sort the elements of the list in ascending or descending order. Default, it will sort in ascending order. After sorting the largest element is at the last position of the list, so we print that element.

Example 1

The following example code finds the largest element of the list using sort() method.

list1 = [100, 1, 5, 80, 20] list1.sort() print("Largest element of the list is:", list1[ 1])

Output

If you execute the above snippet, it produces the following output.

Largest element of the list is: 5

Example 2

Following is another example to find the element from a Python list with a maximum value −

the_list = [54, 57, 827, 74, 91, 74, 62] the_list.sort() maximum_element = the_list [-1] print("The Maximum element of the given list is: ", maximum_element)

Output

Following is an output of the above code −

The Maximum element of the given list is:  827

Using for loop or without using the built in function

In this method, we will not use built-in functions, rather we use a loop to find the largest element in a list.

Here, initially assume the first element as the maximum and then we iterate over a for loop where we compare with other elements in the list. While comparing with elements in the list we change the max variable if the element is greater than the compared maximum element. Finally, after getting terminated from the loop we get the maximum element.

Example

The following is an example code to get the maximum element from a list.

def MaxElement(lst): max = lst[0] for ele in lst: if ele > max : max = ele return max lst = [100, 1, 5, 80, 20] print("Largest element of the list is:", MaxElement(lst))

Output

If you execute the above snippet, it produces the following output.

Largest element of the list is: 100

Updated on: 03-Nov-2023

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements