🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI FreePython max() Function: Find the Largest Value

Python picks (5, 8) over (5, 2). I value getting one winner without asking Python to order every candidate.
When the candidates are records, you may need the winning object for its other fields. Follow max()’s result for a record ranked by key= to see whether it returns the selected item or a comparison value.
What Python’s max() function returns
Python’s built-in max() function returns the greatest item from an iterable or the greatest of two or more separate values. The comparison follows Python’s ordering rules, so max() returns a candidate from the input rather than creating a new value.
A dictionary passed on its own is iterated by key, not by value. Tuple candidates are compared from left to right, which means a later field is considered only when earlier fields compare equal.
- An iterable call returns one item from that iterable.
- Separate positional values return one of those values.
- A key function changes how candidates are compared, not which candidate is returned.
The Python max() documentation lists all three signatures and the rules for key= and default=.
Choose an iterable or separate arguments
Use the iterable form when your candidates are already grouped in a list, tuple, set, or generator. Pass separate arguments when the values are already individual expressions.
| Call form | Example | Result | Available options |
|---|---|---|---|
| One iterable | max([4, 9, 2]) | 9 | key= and default= |
| Two or more values | max(4, 9, 2) | 9 | key= |
Both forms accept key=, but default= belongs only to the iterable form because it provides a value for an empty collection. A single positional argument must itself be iterable, so a lone integer is not a valid candidate form.
Find the greatest item in an iterable
A list works when the values are already collected. A generator expression works when filtering should happen during iteration, and max() returns its greatest surviving value.
scores = [12, 27, 18, 27]
print(max(scores))
print(max(score for score in scores if score < 27))

The first call returns 27 from the list, while the second considers only values below 27. The generator’s maximum is 18.
max() consumes an iterator as it searches. If a later calculation needs the same generated values, build another generator or store them in a collection.
Select a record with key=
Use key= when each candidate is a larger object but you want to compare one field. The function passes each record to the key callable and returns the original record with the greatest key value.
records = [
{"name": "Mira", "score": 84},
{"name": "Ari", "score": 91},
{"name": "Sam", "score": 91},
]
def score(record):
return record["score"]
winner = max(records, key=score)
print(winner)
print(winner["score"])

I found that key=score returns the full dictionary, not 91, because the key controls comparison rather than the result’s shape. Ari and Sam tie, so the first record stays selected.
- Use winner when the rest of the selected record matters.
- Read winner[“score”] when the score itself is the value you need.
To select a dictionary key by its value, pass the dictionary’s get method as key=. AskPython’s dictionary example for selecting a key by value shows the same approach for a mapping.
Handle empty inputs and comparison ties
An empty iterable has no candidate, so max() raises ValueError unless default= supplies a fallback. Mixed values can fail differently when Python cannot compare them.
| Input | What happens | What to do |
|---|---|---|
| Empty iterable without default= | Raises ValueError | Handle the empty case or provide a suitable fallback. |
| Empty iterable with default=None | Returns None | Choose a fallback that cannot be confused with a valid item. |
| Incomparable values such as an integer and a string | Raises TypeError | Normalize the values or define a key that returns comparable results. |
| Several items share the greatest key | Returns the first item encountered | Order candidates first if tie order affects the answer. |
values = []
print(max(values, default=None))
try:
max(values)
except ValueError as error:
print(type(error).__name__ + ":", error)
try:
max([5, "5"])
except TypeError as error:
print(type(error).__name__ + ":", error)

The example catches each exception so it can print both outcomes, and error wording may vary between Python versions. Handle the exception type rather than matching its message.
Ask for one winner or keep the full order
Use max() when you need one candidate. Use sorted() when you need a new list containing every value in order, as shown in AskPython’s Python list-sorting guide.
values = [8, 2, 5]
print(max(values))
print(sorted(values))
print(max([(5, 2), (5, 8), (4, 99)]))
I found max([(5, 2), (5, 8), (4, 99)]) returns (5, 8) because tuple comparison moves to the next field when earlier ones tie. Use key= when your selection depends on a different field.
Python max() questions
The maximum item’s position is a different result from the item itself.
How do I get the index of the maximum item?
Call list.index() with the value returned by max(). If values tie, index() returns the first matching position.


