Six ways to find max value of a list in Python
![]()
Using builtin max() function is most pythonic and the best solution in 99.5% of cases (I checked! ;)). People new to Python might not know that max() takes iterable or list of arguments, and also a key= keyword argument so we can get something like this:
The C-like one:
Not to much to write about… This is the simplest implementation, since it’s written in Python it will be much slower than using the builtin max() which is implemented in C.
The functional one:
In functional languages reduce() is used a lot, in Python3 it was actually moved to a separate module in the standard library — functools . This decision was made to encourage developers to use loops, as they are more readable. There are couple of ways to use reduce in this case:
The second solution is highly unreadable, but it shows an interesting construct using lambda function.
The sometimes useful one:
Heapq is a very useful module for implementing a min queue. For our purposes we can use function heapq.nlargest() which returns a list of n largest values.
Above is equivalent to sorted(A, reverse=True)[:1] (because n = 1) which is yet another solution.
The functional, fancy, but useless one:
This solution uses iterative recursion, besides being highly over complicated (well, most of this solutions are) it’s also very slow and memory-consuming. 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. I wrote a bit more on this topic here: Tail recursion theory made simple.
Can you think of more fancy ways to get a max value from a list?
How to find all positions of the maximum value in a list?
I need to find on which position(s) the maximum value is situated. Please, help.
![]()
18 Answers 18
will tell you the index of the first instance of the largest valued element of list a .
The chosen answer (and most others) require at least two passes through the list.
Here’s a one pass solution which might be a better choice for longer lists.
Edited: To address the two deficiencies pointed out by @John Machin. For (2) I attempted to optimize the tests based on guesstimated probability of occurrence of each condition and inferences allowed from predecessors. It was a little tricky figuring out the proper initialization values for max_val and max_indices which worked for all possible cases, especially if the max happened to be the first value in the list — but I believe it now does.
![]()
I came up with the following and it works as you can see with max , min and others functions over lists like these:
So, please consider the next example list find out the position of the maximum in the list a :
Using the generator enumerate and making a casting
At this point, we can extract the position of max with
The above tells us, the maximum is in the position 4 and his value is 5.
As you see, in the key argument, you can find the maximum over any iterable object by defining a lambda appropriate.
I hope that it contributes.
PD: As @PaulOyster noted in a comment. With Python 3.x the min and max allow a new keyword default that avoid the raise exception ValueError when argument is empty list. max(enumerate(list), key=(lambda x:x[1]), default = -1)
Find Max And Min In A List Python
Working with lists is very common in Python and even more common is to find max and min in a list Python. We will see 3 different methods for each to find min and max in a python list.
A list in python is a collection of user-stored data in order. In a list, data can be of any type like string, integer, float, boolean, etc.
A list can be created by using square brackets [ ] . Example [1, "a", True]
A list can have mixed data or can have only one data type. To find max and min in a list, we will work on a list of numbers. A list that has only integers or float values both negative and positive can be used to find max and min.
Find Maximum Value In List
To find maximum value in a list we will use 3 different methods.
- Using max() function
- Finding the maximum value using for loop
- Using sort() function
1. Using max() function
The max() function is built-in function in Python. It returns the maximum value in a list. It takes a list as an argument and returns the maximum value in the list.
The function accepts the list as an argument. Here is the syntax:
Let's see an example to find the maximum value in a list.
Max in a list of string
The max() function can also be used to find the maximum value in a list of strings.
To compare the values among strings, the max() function uses their ASCII values. For example, the ASCII value of a is 97 and the ASCII value of z is 122.
In the above example, max of the list is e because the ASCII value of e is 101 which is the highest in the list.
Note : max() function does not work on lists of mixed data types.
2. Finding the maximum value using for loop
You can create your own Python function to find the maximum value in a list using for loop and if condition.
Algorithm to find the maximum value in a list
- Create a variable max and assign it to the first element of the list
- Create a for loop to iterate over the list
- Check if the current element is greater than max , if yes then assign it to max . Now current element will become the new max .
- Keep iterating over the list until the end of the list and return max .
The example below is implemented using the above algorithm.
The above example will find the maximum value in the list and print it.
3. Using sort() function: find max
The sort() function is another built-in function in python using which we can find the maximum value in a list. The sort() function sorts the list in ascending order, which means smallest stays at the first position and largest stays at the last position.
The sort() function takes a list as an argument. TO get the maximum value in a list, you can sort the list and picks the last element of the list.
The above example sorts the list and then picks the last element from the sorted list which is the maximum value in the list.

report this ad
Find Minimum Value In List
Again to find the minimum from a list we can use similar 3 methods but this time just to find the minimum value.
- Using min() function
- Finding the minimum value using for loop
- Using sort() function
1. Using min() function
The min() function in python finds the minimum value from a list and returns it. It can work with both numbers and strings as well but not with mixed data types.
In the following example, the min() function finds the minimum value in a list of numbers and prints the output.
Min in a list of string
The min() function can also find the minimum value in a list of strings by comparing their ASCII values.
2. Finding the minimum value using for loop
Creating own function to find minimum value in a list by comparing one value to each other.
Algorithm to find the minimum value in a list
- Store the first element of the list in a variable min
- Now loop through the list element and compare elements from each other. If the current element is less than min , then assign it to min . Now you have the new min value.
- Keep repeating the above steps until the end of the list. At the last min variable will have the actual minimum value of the string.
- Return min .
Here is the implementation of the above algorithm.
The function will find the minimum value in the list and returns it as output.
3. Using sort() function : find min
We can again use the sort() function here to sort the elements of a list in ascending order and then picks the first element of the list which is the minimum value.
The sort() method sorted the element in ascending order which puts the smallest value in the list at the first position which is -28.
Conclusions
In this short guide, we have covered 3 methods for each to find max and min in a list python. We have also covered the min(), max() and sort() as in-built functions in python and also created a custom function to find minimum and maximum.
Stay Ahead, Learn More

report this ad
Python How to Find the Largest Number in a List
To find the largest number in a list in Python:
- Set the first element as the largest number candidate.
- Loop through the list of numbers.
- Update the largest number candidate if a number is greater than it.
Here is how it looks in code:
This is the naive implementation of finding the largest number.
But there are also some useful built-in mechanisms you can use.
In this guide, you learn different ways to find the maximum value in a list in Python.
The max() Function — Find the Largest Element of a List
In Python, there is a built-in function max() you can use to find the largest number in a list.
To use it, call the max() on a list of numbers. It then returns the greatest number in that list.
Here is an example:
Alternative Approaches to Finding the Largest Number in a List
Now you know two straightforward ways to find the largest number in a list in Python.
Let’s take a look at some more uncommon approaches.
Reduce() Function
You can also use the functools reduce() function to find the largest number in a list.
Before we do that, it is important to understand how the reduce() function works.
The reduce function takes two parameters:
- A function that is applied for each element of an iterable.
- An iterable, such as a list.
- Takes the first two elements of a sequence and calls the function on them.
- Takes the previous result, and calls the function on the result and the next number in the list.
- This process continues until there are no elements left in the list.
To learn more about the reduce() function, check this article.
Anyway, let’s use the reduce() function to find the largest element in a list.
Reduce() with the built-in max() Function
Here is an example of how you can use reduce to find the largest number in a list:
The reduce() function applies the max() function for each element as described in the previous chapter.
- It starts with the two first elements and finds the largest of the two
- Then takes the result and compares it with the third element.
- This process continues until there are no numbers left in the list.
Let’s also see another, perhaps a bit more demonstrative example.
Reduce() with a Custom Max Function
Another way you can use reduce() to find the largest number in a list is by implementing the max() function yourself.
Reduce() with a Lambda Function
And the 3rd approach is to use reduce() with a lambda expression.
This means you define the max function inline in the reduce() function call.
The lambda x, y: y if x < y else x part does the same as the my_max() function in the previous example.
Notice that the if-else statement is shortened to a one-liner expression.
Find The Largest Number Using a Heap Queue
The built-in heapq module in Python comes in with an implementation of the priority queue algorithm.
In short, a heap is a binary tree where each parent node has a value less than or equal to the value of its children.

You can use the heapq.nlargest() function to figure out the largest number(s) in a list.
Conclusion
Today you learned how to find the largest number in a list.
First, you used the “brute-force” method to loop through the list while keeping track of the largest element.
Then you saw how the built-in max() function does the job for you.
Finally, you saw uncommon alternatives, such as using reduce() with lambdas or a heap queue.
Each of these approaches gets the job done for you.
Feel free to pick the approach that is best for your situation.
If I had to find the largest element, I would pick the most readable approach, that is, the built-in max() function.