Return the largest item in an iterable or the largest of two or more arguments.
The max() function returns the largest item in an iterable or the largest of two or more arguments. If one positional argument is provided, it should be an iterable, and the largest item in the iterable is returned.
The optional key argument specifies a one-argument ordering function that is applied to each element before comparison. The optional default argument specifies a value to return if the provided iterable is empty. If the iterable is empty and no default is provided, a ValueError is raised.
If multiple items have the maximum value, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as sorted() and heapq.nlargest().
| Name | Description | Optional |
|---|---|---|
iterable |
An iterable of items to compare, or two or more positional arguments. | No |
key |
A function used to extract a comparison key from each element. | Yes |
default |
A value to return if the iterable is empty. | Yes |
max([1, 5, 3]) # Returns 5
max('apple', 'banana', key=len) # Returns 'banana'