Python's Built-in max function

Last updated: Mon. Sep. 14, 2026

This function returns the largest item in an iterable or the largest of two or more arguments. An iterable can be a string, list, tuple, dict, set, etc. If one positional argument is provided, it has to be an iterable and the max() function will return the largest item in the iterable. For example if you run:

print((max([8, 5, 9, 23, 5])))

you will get 23 because that's the greatest item in the list. This also works for characters and sequence if characters known as strings in Python. Suppose you run this program:

print(max(['hi', 'a', 'z', 'hello', 'bye']))

you will get z because that is the largest character or string in the list.

You can pass positional arguments instead of an iterable. If more than one positional arguments are provided, the largest of the positional arguments is returned:

print(max(82, 95, 209))

will return 209 because that is the largest positional argument passed to the function.


This built-in function has two optional keyword-only arguments namely:

  • key

  • default

The key argument specifies a one-argument ordering function like that used for list.sort(). The default argument specifies an object to return if the provided iterable is empty. For instance, running:

print(max([], default='empty'))

will return empty because that's what we told Python to return if the iterable is empty. But what happens if you remove the default keyword argument and passed an empty list?

print(max([]))

It will raise this error - ValueError: max() iterable argument is empty.

« Home