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.

CategoryBuilt-in Functions
KindBuilt-in Function
Python Versionall

📝 Syntax

max(iterable, *[, key, default])
max(arg1, arg2, *args[, key])

⚙️ Parameters

iterable RequiredAn iterable, or the first compared value in the multi-arg form.
args OptionalAdditional values to compare (variadic).
key OptionalA one-arg function used to derive the comparison key.(Default:None)
default OptionalValue returned when the iterable is empty.(Default:None)

Returns:The largest item, or default when the iterable is empty.

💥 Raises

▶ Example

Edit the code, press Run, and see the result in the output panel:

Output:

5
c
apple
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).
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏