In this tutorial, I will explain how to find the maximum value in Python using the max() function. Some asked me this question during a Python webinar, After researching and experimenting with different methods, I discovered several ways to accomplish this task efficiently and I will explain them with examples.
Table of Contents
Syntax of the Python max() Function
The general syntax of the max() function is:
max(iterable, *iterables, key=None)
Or, it can take multiple arguments directly:
max(arg1, arg2, *args, key=None)
Read How to Multiply an Array by a Scalar in Python
Finding the Maximum Value in a Python List of Numbers
One of the most common uses of max()
Is to find the largest number in a list of numeric values. For example, let’s say we have a list containing the populations of major US cities:
city_populations = [2746388, 1603797, 1287401, 1015785, 964586, 885708, 753675, 715522, 689545, 675647]largest_population = max(city_populations)print(f"The largest city population is: {largest_population}")
Output:
The largest city population is: 2746388
I have executed the above example code, you can refer to the screenshot below.

Here the max()
function scans through the city_populations
list and returns the maximum value, which is the population of the largest US city.
Check out How to Initialize an Array in Python
Get the Maximum String Value in Python
max()
is also frequently used to determine the lexicographically “largest” string value, meaning the string that would come last if sorted alphabetically.
Let’s look at an example using a tuple of the names of some well-known USA landmarks:
landmarks = ('Statue of Liberty', 'Golden Gate Bridge', 'Space Needle', 'Grand Canyon', 'Mount Rushmore')last_landmark = max(landmarks)print(f"The 'largest' landmark name is: {last_landmark}")
Output:
The 'largest' landmark name is: Statue of Liberty
I have executed the above example code, you can refer to the screenshot below.

Since “Statue of Liberty” would come last alphabetically out of this tuple of strings, the max(
) function identifies it as the maximum value.
Read How to Find the Closest Value in an Array Using Python
Use max() with a Key Function in Python
Sometimes you may want to find the maximum value based on a particular attribute or criteria of the elements you are evaluating. To do this, you can pass an optional key function to max()
.
For example, let’s find the longest word in a sentence:
speech = "Four score and seven years ago our fathers brought forth on this continent a new nation"longest_word = max(speech.split(), key=len)print(f"The longest word in the Gettysburg Address is: {longest_word}")
Output:
The longest word in the Gettysburg Address is: continent
I have executed the above example code, you can refer to the screenshot below.

Here we first split the sentence into a list of individual words using split()
. Then we pass that list max()
along with the built-in len() function as the key.
We could also use a custom function as the key. Let’s say we have a list of dictionaries containing information about US Presidents and we want to determine which president served the maximum number of terms:
presidents = [ {"name": "George Washington", "terms": 2}, {"name": "John Adams", "terms": 1}, {"name": "Thomas Jefferson", "terms": 2}, {"name": "James Madison", "terms": 2}, {"name": "James Monroe", "terms": 2}, {"name": "Franklin D. Roosevelt", "terms": 4}]def get_terms(pres): return pres["terms"]most_terms_president = max(presidents, key=get_terms)print(f"The president who served the most terms was {most_terms_president['name']} with {most_terms_president['terms']} terms.")
Output:
The president who served the most terms was Franklin D. Roosevelt with 4 terms.
In this example, we defined a custom get_terms()
method that takes in a dictionary and returns the value associated with the “terms” key. We pass this function to max()
use as the sorting criteria. max()
applies get_terms()
to each dictionary in the presidents
list and returns the dictionary for which get_terms()
produces the maximum value.
Check out How to Remove Duplicates from a Sorted Array in Python
Find the Maximum of Multiple Arguments in Python
In addition to accepting an iterable, the max()
Function can also be passed to multiple individual arguments directly. It will return whichever argument has the maximum value.
A real-world scenario where this could be useful is comparing prices from different vendors. Let’s say you are shopping online for a new laptop and want to find the lowest price:
best_buy_price = 999.99amazon_price = 1199.00newegg_price = 1035.50b_and_h_price = 1099.00best_price = min(best_buy_price, amazon_price, newegg_price, b_and_h_price)print(f"The lowest price for the laptop is: ${best_price:.2f}")
Output:
The lowest price for the laptop is: $999.99
By passing the individual price variables directly to min(), we can easily determine which vendor has the best deal on the laptop we want.
Read How to Iterate Through a 2D Array in Python
Handle Empty Iterables
If you try to use max()
on an empty iterable, it will raise a ValueError:
empty_list = []print(max(empty_list))
Output:
ValueError: max() arg is an empty sequence
To avoid this, you can either check if the iterable is empty before calling max()
or specify a default value to return via a second argument if the iterable is empty:
empty_list = []print(max(empty_list, default="Empty!"))
Output:
Empty!
This way you can gracefully handle situations where max() doesn’t have any values to evaluate.
Check out How to Distinguish Between Arrays and Lists in Python
Conclusion
In this tutorial, I helped you learn how to find the maximum value in Python using the max() function. I explained the syntax of the Python max() function
, how to find the maximum value in a Python list of numbers
, how to get the maximum string value
, how to use max() with a key function
. how to find the maximum of multiple arguments in Python
and how to handle empty iterable
.
You may also like to read:
- How to Append to an Array in Python
- How to Create an Array of Zeros in Python
- How to Find the Maximum Value in an Array in Python
Bijay Kumar
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.