How to Find a Maximum Value in a List in Python?

Python provides various inbuilt functions that are used to execute certain tasks in a program. For instance, the inbuilt “max()” and “min()” functions are utilized to find the maximum and minimum values in Python. Other diverse functions are also used to find the maximum value in a list, such as a sort(), reduce() function, etc.

This post will deliver you various methods that are used in Python to find the maximum value in a list using the following contents:

So, let’s start with the first method.

Method 1: Using max() Function

In the example code below, we will use the “max()” function to find the maximum value of the iterable:

Code 1: (Numeric List)

list_value = [22,44,55,66,12]
print(max(list_value))

In the above code, the “max()” function accepts the list variable as an argument and returns the maximum value.

Output:

The above output shows the maximum value in the input list.

Code 2: (List of Strings)

list_value = ['Alex','John','Lily','Joseph']
print(max(list_value, key=len))

In the above code, the “max()” function accepts the “key” parameter as a second argument. The “len” value is assigned to the key, indicating that the string will be compared according to length. The max() function retrieves the max value of the strings.

Output:

The above output shows the string value “Joseph,” the largest value in the input list.

Method 2: Using sort() Method

The sort() method sorts the list in ascending and descending order. The below example shows its working:

Code:

list_value = [22,44,55,66,12]
list_value.sort()
print('Sorted List: ', list_value)
print("Largest Value in List:", list_value[-1])

In the above code, the “list.sort()” is called on the list variable. The last value of the list after sorting is accessed using the statement “list_value[-1]”. The “-1” value indicates that the item of the value placed at the last index will be accessed.

Output:

The above output shows the sorted list and the largest value of the list.

Method 3: Using User-Defined Function

The user-defined function can be defined in the program to find the maximum value in a list. The code below demonstrates the method.

Code:

def maximum(z):
    val = z[0]
    for the item in z:
        if item > val:
            val = item
    return val

z = [4,55,66,77,88,-45]
print(maximum(z))

In the above code, the user-denied function “maximum” is defined in the program using the keyword def. The “maximum()” function will retrieve the largest value of the input list at the time of access.

Output:

The above output shows the largest value in the list.

Method 4: Using reduce() Function

In Python, the “reduce()” function is used to apply a specific function to all the items of the list. In the example given below, the “reduce()” function used the max function as an argument to find the maximum value in a list:

Code:

from functools import reduce
list_value = [45, 66, 22, 12, 32]
print(reduce(max, list_value))

In the above code, the “reduce” function is imported from the “functions” modules at the start of the program. The “reduce()” function accepts the “max()” function name as a first argument and the iterable list variable as a second argument. This function retrieves the max value of the list.

Output:

The above output shows the “66” maximum value in the given list.

Method 5: Using Heap Queue (heapq)

In Python, the “heapq” module provides a “heapq.nlargest()” function to find the largest number of input strings.

Code:

import heapq
list_value = [55,66,77,90,-45]
print(heapq.nlargest(1, list_value))

In the above code, the “heapq.nlargest()” function accepts the largest number and list variable as a first and second argument. A promoter’s “1” value indicates that this function will return only the greatest value. If we used “2” instead of “1”, then the two largest values will be displayed on the screen.

Output:

The above output shows the largest value of the input list “list_value”.

That’s how one can find a maximum value in Python.

Conclusion

In Python, there are five methods to find a maximum value in Python, i.e., max() function, sort() method, user-defined function, reduce() function, and heap queue. This Python post presented a detailed guide on how to find the maximum value in a list.