max() Maximum value
Last updated: 2026-09-22
max() Maximum value
max() returns the largest item of an iterable (or of multiple args); accepts a key function.
| Category | Built-in Functions |
|---|---|
| Kind | Built-in Function |
| Python Version | all |
📝 Syntax
max(iterable, *[, key, default])
max(arg1, arg2, *args[, key])
⚙️ Parameters
| iterable Required | An iterable, or the first compared value in the multi-arg form. |
|---|---|
| args Optional | Additional values to compare (variadic). |
| key Optional | A one-arg function used to derive the comparison key.(Default:None) |
| default Optional | Value returned when the iterable is empty.(Default:None) |
Returns:The largest item, or default when the iterable is empty.
💥 Raises
ValueError— Raised when the iterable is empty and no default is given.TypeError— Raised when items cannot be compared.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
5
c
apple
🏷️ Related Built-in Functions
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat happens with max([])?
AIt raises ValueError: max() arg is an empty sequence; pass a default to avoid it: max([], default=0) returns 0.
QWhat does key do in max(lst, key=len)?
AIt sets the comparison basis: max(['apple', 'ant'], key=len) returns 'apple' (the longest). key is only used for comparing; the returned value is still the original element.
QWhat is the difference between max(1, 5, 3) and max([1, 5, 3])?
AThe result is the same (both are 5). The former is the multi-argument form (comparing the arguments directly); the latter is the single-argument form (comparing every element of the iterable).
QWhat does max() return when given a dict?
AIt returns the largest key; to find the key with the largest value use max(d, key=d.get).