Python max()

Table of Contents

Introduction

Python provides the max() function to find the maximum (or largest) element among a group of values.
This function is built-in and you can find additional information reading the official Python documentation.

In the next sections we will see in detail how to use the max() function with examples.

Are you curious to find out how to find the smallest element instead? Look at this tutorial!

Python max() function

If you have a list of values and you need to find the maximum of them, the max() function is the one for you!

Python provides the max() function to find the maximum (or largest) value in an iterable or among a set of arguments.
In fact, there are two ways to use this function.

The first one is to pass an iterable that is a list, a tuple or a dictionary as an argument.

max(<my_iterable>)

Alternatively you can pass to the max() function directly the group of values you want to find the maximum among.

max(<elem_1>, <elem_2>, <elem_3>, ... )

In both cases, this functions returns the maximum value of the iterable or arguments.

In the next sections we will see examples of use for both cases.

Python max() passing as argument an iterable

The first feature I want to show you is how to find the largest (or widest) value in an iterable.
I remind you that an iterable is a data structure such as a list, a tuple or a dictionary.

Let’s not waste any more time and see immediately how it works using some concrete examples.

List

my_list = [1, 2, 4, 10, 3, 0]
print(max(my_list))

Output

10

And what about a list of strings?

my_list = ["aaaaaaaaaaaaa", "a", "zzzzz"]
print(max(my_list))

Output

'zzzzz'

In this case the max() function returns the largest element ordered alphabetically.

Tuple

my_tuple = (1, 2, 4, 10, 3, 0)
print(max(my_tuple))

Output

10

Dictionary

my_dict = {1: "first_value", 3: "second_value", 0: "third_value"}
print(max(my_dict))

Output

3

As you can see, in this case the maximum value is taken from the keys of the dictionary and not the values.

It is also possible to search which key has the greatest value using this code:

my_dict = {1: "first_value", 3: "second_value", 0: "third_value"}
print(max(my_dict, key = lambda k: my_dict[k]))

Output

# key with largest value
0

At this point it is easy to find the largest value by accessing the dictionary by key:

my_dict = {1: "first_value", 3: "second_value", 0: "third_value"}
# get key with largest value
key_with_largest_value = max(my_dict, key = lambda k: my_dict[k])
# get largest value
print(my_dict[key_with_largest_value])

Output

third_value

Possible exceptions

If you use the max() function without any iterable, a ValueError exception is raised.

max([])

Output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: max() arg is an empty sequence

Python max() passing multiple arguments

It is possible to use the max() function without integrable but directly passing the elements as parameters.
Here are a couple of examples of use with integers, floats and strings.

# integers
print(max(2, 3, 6, 0, 4))
# floats
print(max(3.7, 3.0, 0.6, 3.33333, 2.56))
# strings
print(max("hello", "how", "are", "you"))

Output

6
3.7
'you'

Possible exceptions

If you use the max() function without any argument, a TypeError exception is raised.

max()

Output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: max expected 1 arguments, got 0

Conclusion

Here we are at the end of this tutorial.
As always I hope you are clear and now you know how to best use the max() function in Python.
If you have any doubts or errors, do not hesitate to let me know in the comments.

Leave a Comment

Your email address will not be published. Required fields are marked *