Signup/Sign In

Find Maximum Value in List in Python

In this article, we will learn to find the maximum value in a list in Python. We will use some built-in functions, simple approaches, and some custom codes as well to understand the logic. Let's first have a quick look over what is a list in Python and how can we find the maximum value or largest number in a list.

Python List

Python has a built-in data type called list. It is like a collection of arrays with different methodology. Data inside the list can be of any type say, integer, string or a float value, or even a list type. The list uses comma-separated values within square brackets to store data. Lists can be defined using any variable name and then assigning different values to the list in a square bracket. The list is ordered, changeable, and allows duplicate values. For example,

list1 = ["Ram", "Arun", "Kiran"]
list2 = [16, 78, 32, 67]
list3 = ["apple", "mango", 16, "cherry", 3.4]

Let us look at the below Python examples one by one to find the largest item in a list of comparable elements using the following methods-

  1. Using built-in max() function
  2. Brute Force Approach
  3. Using reduce() function
  4. By implementing the Heap Queue algorithm
  5. Using sort() function
  6. Using sorted() function
  7. By Tail Recursive algorithm

Example: Find the maximum value using max() Function

This is the simplest and straightforward approach to find the largest element. The Python max() function returns the largest item in an iterable. It can also be used to find the maximum value between two or more parameters.

The below example uses an input list and passes the list to max function as an argument.

list1 = [3, 2, 8, 5, 10, 6]

max_number = max(list1);

print("The largest number is:", max_number)


The largest number is: 10

If the items in the list are strings, first they are ordered alphabetically and then the largest string is returned.

string = ["Find", "maximum", "string", "from", "list"]

max_string = max(string, key=len)

print("The largest string is:", max_string)


The largest string is: maximum

Example: Find the maximum value using Brute Force Approach

This is the simplest implementation but a bit slower than the max() function because we use this algorithm in a loop.

def large(arr):

    #root element varible 
    max_ = arr[0]
    for ele in arr:
        if(ele > max_):
           max_ = ele
    return max_ 


list1 = [1,4,5,2,6]
result = large(list1)
print(result)


6

The above example has defined a function large() to find maximum value and it takes the input list as the argument. In this approach, a variable is used initially to store the first element of the list. Under for loop, each element is compared with this root element variable. If the element is greater than the root element, we assign the value of this item to the root element variable, and at last, after comparing we get the largest element.

Example: Find maximum value using reduce() function

In functional languages, reduce() is an important function and is very useful. In Python 3 reduce() function is moved to a separate module in the standard library called functools. This decision was made to encourage developers to use loops, as they are more readable. Lets us see the below example to use reduce() in two different ways in this case:

The reduce() takes two parameters. The first is the keyword max which finds the maximum number and the second argument is iterable.

from functools import reduce

list1 = [-1, 3, 7, 99, 0]

print(reduce(max, list1))


99

The second solution shows an interesting construct using lambda function. The reduce() takes the lambda function as an argument and the lambda() function takes two arguments. It takes a condition and input list to check for maximum value.

from functools import reduce

list1 = [-1, 3, 7, 99, 0]

print(reduce(lambda x, y: x if x > y else y,list1))


99

Example: Find maximum value by using the Heap Queue

This module provides an implementation of a heap queue algorithm known as heapq. Heapq is a very useful module for implementing a minimum queue. The important property of the heap is that its smallest element is always the root element. Using the given example, we use heapq.nlargest() function to find the maximum value.

import heapq

list1 = [-1, 3, 7, 99, 0]

print(heapq.nlargest(1, list1))


[99]

The above example, imports heapq module and takes an input list. The function takes n=1 as the first argument because we need to find one maximum value and the second argument is our input list.

Example: Find maximum value using sort() Function

This method uses the sort() function to find the largest element. It takes a list of values as input then sorts the list in ascending order and prints the last element in the list. The last element in the list is list[-1].

#input list
list1 = [10, 20, 4, 45, 99]

# sorting the list
list1.sort()

# print last element
print("Largest element is:", list1[-1])


Largest element is: 99

Example: Find maximum value using sorted() Function

This method uses sorted() function to find the largest element. It takes a list of values as an input. Then, the sorted() function sorts the list in ascending order and print the largest number.

list1=[1,4,22,41,5,2]

sorted_list = sorted(list1)

result = sorted_list[-1]

print(result)


41

Example: Find the maximum value using Tail Recursive Algorithm

This method is not handy and sometimes programmers find it useless. This solution uses iterative recursion, and its working is highly over complicated. It is also a very slow and memory-consuming program. This is because unlike pure functional languages, Python doesn’t optimize for tail recursion, so every call to max() is being held on the stack.

def func(arr, max_=None):
    if(max_ is None):
        max_ = arr.pop()
    current = arr.pop()
    if(current > max_):
        max_ = current
    if(arr):
        return func(arr, max_)
    return max_

list1=[1,2,3,4,2]
result = func(list1)
print(result)


4

Conclusion

In this article, we learned to find the maximum value from the given input list by using several built-in functions such as max(), sort(), reduce(), sorted() and other algorithms as well. We used some custom codes to understand the brute approach, tail recursion method, and heap algorithm. The tail-recursive algorithm is not generally in use or practice but for different implementation checks, you can read further about it.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.