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:
heights = [100, 2, 300, 10, 11, 1000] largest_number = heights[0] for number in heights: if number > largest_number: largest_number = number print(largest_number)
Output:
1000
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:
heights = [100, 2, 300, 10, 11, 1000] max_height = max(heights) print(max_height)
Output:
1000
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.
reduce(function, iterable)
The reduce function takes two parameters:
It then:
- 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:
from functools import reduce heights = [100, 2, 300, 10, 11, 1000] max_height = reduce(max, heights) print(max_height)
Output:
1000
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.
For example:
from functools import reduce heights = [100, 2, 300, 10, 11, 1000] def my_max(x, y): if x < y: return y else: return x max_height = reduce(my_max, heights) print(max_height)
Output
1000
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.
For example:
from functools import reduce heights = [100, 2, 300, 10, 11, 1000] max_height = reduce(lambda x, y: y if x < y else x, heights) print(max_height)
Output:
1000
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.
For example:
import heapq heights = [100, 2, 300, 10, 11, 1000] max_height = heapq.nlargest(1, heights)[0] print(max_height)
Output:
1000
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.
Thanks for reading. I hope you find it useful.
Happy coding!