Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Function

max

max() returns the largest item from an iterable or the largest among two or more positional arguments. Use key to control ordering and default to handle an empty iterable.

Since
3.4

How it works

Use max() with one iterable argument to obtain its greatest item, or pass at least two positional values to compare those values directly. The keyword-only key function determines comparison values, while default supplies a result when an iterable has no items.

Syntax

max(iterable, *, key=None, default=?)
max(arg1, arg2, *args, key=None)

Parameters

NameTypeDescription
iterable / arg1iterable or objectAn iterable of candidates, or the first candidate when comparing multiple positional arguments.
arg2, *argsobjectAdditional candidates to compare in the multiple-argument form.
keycallable or NoneA one-argument function used to determine ordering.
defaultobjectFallback returned when the iterable form receives no items.

Returns

The largest item or argument according to the ordering rules; with key, it returns the original item associated with the greatest key result.

Key points

  • max() supports either an iterable form or a comparison among two or more positional arguments.
  • The one-argument form treats its argument as the source of candidate items.
  • The multiple-argument form returns the greatest positional candidate.
  • Use key to define how candidates are ranked, such as selecting by length.
  • Use default to make the iterable form return a chosen fallback for empty input.

Common mistakes

  • An empty iterable raises ValueError unless default is supplied.
  • Ties preserve the first maximal item encountered.

Example: Selecting maximum values

scores = [72, 91, 84]
print(max(scores))
print(max(scores, default=0))
print(max(["Ada", "Grace", "Linus"], key=len))

The first call selects the greatest number, the second supplies a fallback for an empty iterable, and the third compares strings by their lengths.

When to use it

Find a maximum

Choosing the greatest numeric value from a collection.

Rank with key

Selecting an object by a derived property.

Provide a fallback

Making empty-input processing safe.

Compare arguments

Comparing several independent candidate values.

Interview questions

QHow do the two positional-argument forms of max() differ?

With one positional argument, max() expects an iterable and returns its largest item. With two or more positional arguments, it compares those arguments directly.

Follow-up: How can max() choose the longest string? Pass key=len; max() compares the values returned by that one-argument function.

QWhat does the key parameter do?

key is a one-argument ordering function. It transforms each candidate into a comparison value without changing which original item max() returns.

Follow-up: What happens when several items have equal key results? The first maximal item encountered is returned.

QWhen should default be supplied?

default is a keyword-only fallback used when the single iterable argument is empty. Without default in that situation, max() raises ValueError.

Follow-up: Can default be used with several positional candidates? No; it applies to the iterable form, not the multiple-argument form.

  • min()
  • sorted()
  • list.sort()
  • heapq.nlargest()

Source: Built-in Functions — Python 3.11.17 documentation. Checked 2026-10-15.