transformation

max()

Returns the largest item in an iterable or the largest of two or more arguments. Supports a key function for custom comparison.

max(iterable, *, key=None, default=...)  |  max(arg1, arg2, *args, *, key=None)

Parameters

Parameter Purpose
key Function extracting a comparison key
default Value returned when iterable is empty (else ValueError)

Examples

max([3, 1, 4, 1, 5])

Returns 5

max([{'n': 1}, {'n': 3}], key=lambda d: d['n'])

Returns {'n': 3}

max('hello')

Returns 'o' (highest codepoint)

Gotcha

On ties, returns the first equal-key element encountered (stable). Empty iterable without default raises ValueError.

Related built-ins

← All Python built-ins