max()

The largest item of an iterable — or of several arguments — with an optional key function.

Built-in functionPython 1.0+Live demo
Common call
max(scores)
Returns
the item itself, not its index
Replaces
strings compare lexicographically: max(['10', '9']) is '9'
Watch out
empty iterable raises ValueError — pass default= to survive it
max(iterableiterableThe items to compare. Alternatively pass two or more positional arguments.type: iterable · required, *, keykeyOne-argument function computing the comparison key, e.g. key=len.type: callable · default: None=None, defaultdefaultReturned when the iterable is empty. Without it, empty raises ValueError.type: Any · default: (none)=...)
Any

Demo

Live evaluation
Try:
Inputs
itemslistcomma-separated items
Output
['banana', 'apple', 'cherry'].max()
'cherry'

The demo’s comma-separated items are strings, so comparison is lexicographic — that is why ’9’ beats ’10’. Pass key=int in real code for numeric strings. An empty iterable raises exactly Python’s error.

Parameters

NameTypeRequiredDescription
iterableiterableyesThe items to compare. Alternatively pass two or more positional arguments.
keycallableno (None)One-argument function computing the comparison key, e.g. key=len.
defaultAnyno ((none))Returned when the iterable is empty. Without it, empty raises ValueError.

Return value

AnyThe largest item. Empty iterable raises ValueError unless default is given. Also callable as max(a, b, ...).

Common patterns

Largest by a computed key
key gives you argmax behavior — the ITEM with the biggest key.
longest = max(words, key=len)
oldest  = max(users, key=lambda u: u.age)
Safe max of possibly-empty data
default turns the empty-sequence error into a value.
best = max(scores, default=0)
Clamping a value
The two-argument form pairs with min for range clamps.
clamped = max(lo, min(x, hi))

Examples

1. Max of an iterable
max([3, 1, 4, 1, 5])
Returns
5
2. Strings — lexicographic
max(["10", "9", "2"])
Returns
'9'
3. Two-argument form
max(3, 7)
Returns
7
4. Empty with default
max([], default=0)
Returns
0

Pitfalls

1. Empty iterable raises
Aggregating possibly-empty data needs the default keyword.
Raises
max([])
ValueError: max() arg is an empty sequence
Fix
max([], default=0)
0
2. Digit strings compare as text
Same trap as sorted — character order, not numeric order.
Surprising
max(["10", "9"])
'9'
Fix
max(["10", "9"], key=int)
'10'
3. max of a dict gives the largest KEY
Iterating a dict yields keys — use key= or .items() for value-based max.
Keys compared
max({"a": 3, "b": 1})
'b'
Largest by value
max(d, key=d.get)
'a'

When to use

Use it
  • Largest item, or item with the largest key (argmax)
  • Pairwise comparisons via the multi-argument form
  • Clamping combined with min
Reach for something else
  • Top-k items → heapq.nlargest
  • Max under a condition → max(filter(...), default=...)
  • Running maximum in a loop → track it yourself (one pass)

Notes

Complexity
O(n) — single pass
Return
an item from the input (not a copy)
CPython impl
Python/bltinmodule.c :: builtin_max
Memory
No allocation
Thread-safe
Yes for the scan; the source should not mutate concurrently

FAQ

Combine with index, or take the argmax over enumerate.

i = lst.index(max(lst))
# or, one pass:
i = max(range(len(lst)), key=lst.__getitem__)

History

3.4
The default keyword argument added.
2.5
The key keyword argument added.