Kodeclik Blog

Python’s max function

The max() function in Python is one of the easiest to learn and use. It is also very versatile. It takes any type of iterable (e.g., a list) as input and returns the maximum value in that iterable.
Let us first try using this function with a list as input.

Python max with list iterable

Consider for instance:
This outputs, as expected:
Let us try it with a list of strings:
The output is:
The reason wall is printed is because by default, max on a list of strings returns the string that has the lexicographically highest value. In other words, sort the strings by the lexicographic order and pick the string that is at the end. Let us try:
This gives:

Python max with string

Because max takes an iterable as input, a string is a valid input. Let us try:
We get:
This is because the individual characters of the string, when sorted, are organized into “K”, “c:, “d”, and so on with “o” being the lexicographically last character.

Python max with list iterable and custom sorting function

You are not restricted to using Python defaults for sorting. We can specify a different comparison operator to use with the max() function. For instance, in the earlier example of sorting the elements of a list, we can use length of the element as the criterion to sort the elements:
The output is:
Recall that earlier the result of this max() function was “wall”. Now it is “Humpty” because we are seeking the (string) element that has the longest length.

Python max with individual objects

We can also pass to max, not an iterable, but individual objects. For instance:
This gives as output:
We can mix integers and floats in the input:
This gives as output:
On the other hand, if you try:
We get:
This is because the comparison operator does not know to compare an integer against a string. (But it works when you mix integers and floats).
Just as we customized how elements are sorted, we can do the same when passing individual objects:
This outputs:
In the first line, we are arranging the elements by length and thus “Humpty” is returned. In the second line, there is no “key” variable specified and as a result the default is lexicographic ordering and thus the max value returned is “hello”.

Python max with a dictionary

Let us create a dictionary and apply the max() function on it:
This outputs:
Thus we can see that by default, max() applied to a dictionary uses the key values to sort. If we wish to sort not by the keys but by the associated values, we can define a key parameter and pass on a function to it. Let us suppose we want to sort by the value of the (month) strings so that the max() function should return “September” since the month starting with “S” is the lexicographically largest. If we try:
we will get the error:
This is because by default the function specified in key is being applied to the dictionary’s keys, not to the dictionary’s values. We can fix this problem by defining a lambda function:
This gives, as expected:
because the month with key=9 has the value “September”. To find the actual month, we can do:
This will output:
Instead of sorting the dictionary’s values lexicographically, we can try to sort it by the length of the string.
This outputs:
How come? This is because “September” is also the string with the largest number of characters, in addition to being the string that begins with lexicographically largest value. To confirm that this lambda function is working as intended, let us artificially change a month’s name:
This will now output:
confirming that the function is working as intended.
As you have learnt in this blogpost, the max() function in Python is a very versatile function, applicable both to individual values as well as to values in an iterable. Furthermore, you can customize the default behavior of the max() function by providing your own way to rank order the input values.
If you liked the Python max() function, checkout our blogpost on the Python min() function.
Interested in more things Python? See our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2024. All rights reserved.