max
Find the largest item of an iterable or the largest of several arguments.
Python 3.13
max(iterable, *, key=None[, default])
max(arg1, arg2, *args, key=None)- [ ]optional part
- =default value
- *arguments after it by name only
- iterable
- The items to search for the maximum.Iterable
- arg1, arg2, *args
- Two or more values to compare instead of iterable.Any
- key
- A function whose result is used to compare items, such as len; without it, the items themselves are compared.Callable | NoneOptional
- default
- What to return if iterable is empty; works only with iterable.AnyOptional
Return value
The largest item; raises ValueError for an empty iterable without default.
Examples
Largest number in a list
Python 3.13
print(max([10, 20, 5]))20
Longest word
Python 3.13
print(max(['banana', 'kiwi', 'apple'], key=len))banana
Default value for an empty list
Python 3.13
print(max([], default=0))0
Details
If several items are equally large, the first of them is returned.
