max()
Returns the largest item of a list or of several arguments, with a key function to choose what to compare and a default for empty data.
max(iterable, *, key=None)
max(iterable, *, default, key=None)
max(arg1, arg2, *args, key=None)max() finds the largest value, either in one collection, max([4, 11, 7]), or among separate values, max(4, 11, 7). Both give 11.
With key you choose what to compare: max(words, key=len) gives the longest word, and max(scores, key=scores.get) gives the name with the highest score in a dictionary. If several items tie, the first one is returned.
Watch out for numbers stored as text: max(["9", "10"]) is "9", because strings are compared character by character and "9" comes after "1". Convert them or use key=int. An empty collection raises ValueError unless you pass default.
Parameters
| Parameter | What it is |
|---|---|
iterable or arg1, arg2, …required | One collection to look through, or two or more separate values. |
keyoptional, default None | A function applied to each item; items are compared by its results, but the item itself is returned. |
defaultoptional | What to return when the iterable is empty. Only for the one-collection form. |
Return value
The largest item itself (or default for an empty iterable).
Examples
1Largest number
print(max(4, 11, 7))
print(max([2.5, 9.75, 3]))
print(max(-8, -3))11 9.75 -3
2Winner in a dictionary of scores
scores = {"Ana": 88, "Ben": 95, "Chen": 91}
winner = max(scores, key=scores.get)
print(winner, scores[winner])Ben 95
3Longest word, first one wins a tie
words = ["sun", "planet", "galaxy", "moon"]
print(max(words, key=len))planet
planet and galaxy both have 6 letters; max() returns the first.
4Cap a value with max() and min()
A common trick is to keep a number inside a range: never below 0 and never above 100.
def clamp(value, low=0, high=100):
return max(low, min(value, high))
for v in [-20, 45, 130]:
print(v, "->", clamp(v))-20 -> 0 45 -> 45 130 -> 100
5Most expensive order of the day
orders = [
{"id": 101, "total": 34.90},
{"id": 102, "total": 120.00},
{"id": 103, "total": 58.40},
]
biggest = max(orders, key=lambda o: o["total"])
print(biggest["id"], biggest["total"])
print(max([], default=0))102 120.0 0
Common mistakes
Numbers stored as text
Strings compare character by character, so the result can look wrong. Use key=int or convert the values.
versions = ["9", "10", "2"]
print(max(versions))
print(max(versions, key=int))9 10
Calling max() on an empty list
An empty collection has no largest item, so max() raises ValueError. Pass default= when the data may be empty.
bids = []
try:
print(max(bids))
except ValueError:
print("ValueError: max() of an empty list")
print(max(bids, default="no bids yet"))ValueError: max() of an empty list no bids yet
Questions people ask
How do I find the max value in a list in Python?
max(my_list).
How do I get the key with the highest value in a dictionary?
max(d, key=d.get). For the pair: max(d.items(), key=lambda kv: kv[1]).
How do I get the index of the largest item?
my_list.index(max(my_list)) gives the first position of the largest value.
How do I find the longest string in a list?
max(strings, key=len).