Python max() Function
max(iterable) or max(a, b, ...) returns the largest item; use key= to choose what to compare and default= to avoid an error on empty input.max() finds the highest score, the most expensive product, the longest word or the latest value. It takes either one iterable or several separate arguments, so max(3, 8) and max([3, 8]) both work. It is the mirror image of min().
For the second-largest value and other patterns, see the largest number in a list program and the sorting lesson.
| Syntax | max(iterable, *, key=None, default=<no default>) or max(arg1, arg2, *args, key=None) |
|---|---|
| Parameters | iterable or several arguments; key: function giving the value to compare; default: result for an empty iterable (keyword-only) |
| Returns | The largest item itself (not its index) |
| Raises | ValueError on empty input without default; TypeError if items cannot be compared |
How max() compares numbers, strings and tuples
Numbers are compared by value, and strings alphabetically by Unicode code point, which means lowercase letters beat uppercase ones. Tuples compare element by element. With ties, max() returns the first of the equal largest items.
print(max(7, 3, 9))
print(max([4.5, 2, 8]))
print(max("Zebra", "apple"))
print(max("python"))Using key= to find the longest word or best record
With key, each item is scored by your function and the item with the highest score is returned unchanged. This is the standard way to get the top student, the longest string or the dictionary key with the biggest value.
words = ["banana", "fig", "cherry"]
print(max(words, key=len))
marks = {"Asha": 88, "Ravi": 95, "Meera": 91}
print(max(marks, key=marks.get))max() with an empty iterable and a default value
max([]) raises ValueError, which often happens when a filter removes every item. Use max(values, default=0) to return a fallback instead. Remember that default cannot be combined with separate positional arguments.
Examples you can run
Press Run, then edit the code and run it again.
Highest score and most expensive item
Use max() with a list and with separate arguments.
scores = [72, 88, 95, 61]
print(max(scores))
print(max(12.5, 9.99, 15))Expected output95
15
Find the top student with key
The key function picks the score, the full record is returned.
students = [("Asha", 88), ("Ravi", 95), ("Meera", 91)]
best = max(students, key=lambda s: s[1])
print(best)
print(best[0], "scored", best[1])Expected output('Ravi', 95)
Ravi scored 95
Largest value among those that pass a filter
The generator may be empty, so default keeps the program safe.
scores = [40, 55, 38]
passed = (s for s in scores if s >= 60)
print(max(passed, default="nobody passed"))Expected outputnobody passed
Common mistakes
Calling max() on an empty list
✗ Wrong
print(max([]))✓ Right
print(max([], default=0))Check the list or pass default=.
Mixing types
✗ Wrong
print(max(3, "7"))✓ Right
print(max(3, int("7")))Convert the values so they have the same type.
Expecting the index instead of the value
✗ Wrong
scores = [72, 95, 61]
print(max(scores) == 1)✓ Right
scores = [72, 95, 61]
print(scores.index(max(scores)))Use scores.index(max(scores)) for the position, or max(range(len(scores)), key=scores.__getitem__).
Python max() Function: questions people ask
How do I find the largest number in a list in Python?
Use max(my_list), for example max([4, 1, 7]) returns 7.
How do I get the key with the highest value in a dictionary?
Use max(d, key=d.get). It returns the key whose value is largest.
What does max() return for strings?
The string that comes last alphabetically by character code. Use key=len to get the longest string instead.
Keep learning
Step-by-step lessons:
Python ListsPython Sorting
Related:
Python min() FunctionPython sum() FunctionPython sorted() FunctionPython Program to Find the Largest Number in a ListPython Program to Find the Second Largest Number in a List
More in built-in functions:
Python print() FunctionPython len() FunctionPython range() FunctionPython type() FunctionPython int() FunctionPython float() FunctionPython str() FunctionPython input() Function See all →