Built-in Functions and Types

max()

Select the largest item using ordinary ordering or an optional comparison key. The result is the original item, not the computed key. When items tie, the first encountered wins. An empty iterable needs default to avoid ValueError; default is unavailable in the multiple-positional-arguments form. Items or computed keys must be mutually orderable.

Formal Syntax & Structure

python syntaxpython
max(iterable, /, *, key=None)max(iterable, /, *, default, key=None)max(arg1, arg2, /, *args, key=None)

Parameters & Attributes

NameTypeDescription
iterableiterableSingle-argument form; items to compare.
arg1objectFirst candidate in the multiple-argument form.
arg2objectSecond candidate in the multiple-argument form.
argsobjectAdditional positional candidates in that form.
keycallable | NoneKeyword-only function mapping each item to its comparison value.
defaultobjectKeyword-only fallback for an empty iterable; omission means no fallback.

Return Value

object

The largest original item, or the supplied default if the iterable is empty.

Working Example

Example Snippetpython
locations = [("west", 8), ("east", 12), ("north", 12)]chosen = max(locations, key=lambda row: row[1])print(chosen[0])print(max([], default="none"))
Python Reference