max()
max(iterable, *[, key, default]) Returns:
object · Updated March 13, 2026 · Built-in Functions built-in comparison iteration
The max() function returns the largest item in an iterable or the largest of two or more arguments. It accepts an optional key parameter for custom comparison logic and a default value for empty iterables.
Syntax
max(iterable, *[, key, default])
max(arg1, arg2, *args)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| iterable | iterable | — | An iterable containing comparable elements |
| *args | objects | — | Multiple arguments to compare (instead of iterable) |
| key | callable | None | A function that returns a value to compare |
| default | object | None | Value to return if the iterable is empty |
Examples
Basic usage with a list
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
result = max(numbers)
print(result)
# 9
Multiple arguments
result = max(3, 1, 4, 1, 5, 9, 2, 6)
print(result)
# 9
Using key parameter
words = ["apple", "banana", "cherry", "date"]
longest = max(words, key=len)
print(longest)
# 'banana'
Using default for empty iterable
empty_list = []
result = max(empty_list, default=0)
print(result)
# 0
Finding maximum in a list of dictionaries
students = [
{"name": "Alice", "grade": 85},
{"name": "Bob", "grade": 92},
{"name": "Charlie", "grade": 78}
]
top_student = max(students, key=lambda x: x["grade"])
print(top_student)
# {'name': 'Bob', 'grade': 92}
Common Patterns
Finding the key with maximum value
word_lengths = {"apple": 5, "banana": 6, "cherry": 6}
longest_word = max(word_lengths, key=word_lengths.get)
print(longest_word)
# 'banana'
Conditional max
numbers = [1, -5, 3, -2, 7, -1]
max_positive = max(n for n in numbers if n > 0)
print(max_positive)
# 7
Errors
-
ValueError: If the iterable is empty and no default is provided:
max([]) # ValueError: max() arg is an empty sequence -
TypeError: If comparing incompatible types:
max([1, "a"]) # TypeError: '>' not supported between instances
See Also
- built-in::min — returns the smallest item
- built-in::sorted — returns a sorted list (ascending by default)