A small report summarizes a collection without creating additional indexing logic.
What You Need to Understand
max() finds the largest value in an iterable or among several arguments. It works on an iterable and is often clearer than writing a manual accumulation or search loop.
When to Use It
A small report summarizes a collection without creating additional indexing logic.
Step-by-Step Python Example
values = [7, 2, 9]
print(max(values))
print(max(["pear", "fig", "banana"], key=len))
How the Example Works
The largest integer is 9, and the longest word by length is banana.
Example Output
9
banana
3
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
values = (value for value in [1, 2, 3])
print(max(values))
Important Behavior and Edge Cases
An empty iterable raises ValueError without default; a largest key is not necessarily a largest original value.
Try It Yourself
Adapt the values and predict the result, then verify it in Python.
Common Beginner Mistakes
- An empty iterable raises
ValueErrorwithoutdefault; a largest key is not necessarily a largest original value. - Overlooking how an iterable is consumed, how element positions are chosen, or whether a function creates a new list.
Key Takeaways
max()finds the largest value in an iterable or among several arguments.- The largest integer is 9, and the longest word by length is
banana. - An empty iterable raises
ValueErrorwithoutdefault; a largest key is not necessarily a largest original value.