Python max() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in

Definition

In Python, max() returns the largest item, either of one iterable, max([3, 11, 7]), or of two or more arguments, max(3, 11, 7).

Like min(), it accepts a key function to choose what is compared, returns the first of several equal maximums, and accepts default for an empty iterable. Strings, tuples, and lists compare item by item from the left.

max() is a built-in function: it lives in the builtins module, so it is always available without an import.

Syntax

Use the same call shapes as min().

python
largest = max(iterable, key=None)
largest = max(a, b, c)
largest = max(iterable, default=value)
CallWhat it does
max(3, 11, 7)Returns 11.
max(scores, key=abs)Returns the score farthest from zero.
max(d, key=d.get)Returns the dict key with the largest value.
max(low, min(x, high))Clamps x into the range low..high.

max(5) is an error: with one argument, max() expects an iterable, and an int is not iterable.

Library declarations
python
max(iterable, *, key=None)
max(iterable, *, default, key=None)
max(arg1, arg2, *args, key=None)

Parameters

ParameterDescription
iterableAn iterable of comparable items (one-argument form).
arg1, arg2, *argsTwo or more values to compare.
keyOptional, keyword-only. Function whose result is compared.
defaultOptional, keyword-only. Returned when the iterable is empty.

Return Value

Return ValueDescription
itemThe largest item itself; the first one on ties.

Python Examples

Example 1 — Largest Number, Letter, and List

See how max() compares ints, floats, characters of a string, and whole lists.

python
print(max(3, 11, 7))
print(max([0.5, 2.75, 1.5]))
print(max("hello"))
print(max([1, 5], [1, 4, 9]))

How It Works

  1. Numbers. The biggest argument, 11, and the biggest float in the list, 2.75, are returned.
  2. Letters. In "hello", "o" has the highest code point (111).
  3. Lists left to right. [1, 5] and [1, 4, 9] tie on the first item; 5 beats 4 at the second, so length does not matter.
  4. Whole item back. max() returns the winning list itself, not just the deciding element.

Example 2 — Top Student, Most Common Word, Longest Name

Use key with records, a dictionary, and len.

python
students = [
    {"name": "Ravi", "score": 88},
    {"name": "Mei", "score": 95},
    {"name": "Tom", "score": 95},
]
top = max(students, key=lambda s: s["score"])
print("Top student:", top["name"])

word_counts = {"apple": 4, "kiwi": 9, "plum": 2}
most = max(word_counts, key=word_counts.get)
print("Most common:", most, word_counts[most])

longest = max(["red", "yellow", "blue"], key=len)
print("Longest:", longest)

How It Works

  1. Score as key. The lambda pulls out each student's score; the dict with the highest score is returned.
  2. First of a tie. Mei and Tom both have 95, and Mei comes first, so Mei is the top student.
  3. Dict lookup key. key=word_counts.get compares the counts and returns the key "kiwi".
  4. Length key. "yellow" has 6 letters, more than "red" or "blue".

Example 3 — Clamp a Value into a Range

Combine max() and min() to keep a value between limits, then look at empty and single-int calls.

python
def clamp(value, low, high):
    return max(low, min(value, high))


for v in [-5, 42, 150]:
    print(v, "->", clamp(v, 0, 100))

print(max([], default=0))
try:
    max(5)
except TypeError:
    print("max(5) fails: a single int is not iterable")

How It Works

  1. Upper limit. min(value, high) caps 150 at 100.
  2. Lower limit. max(low, ...) lifts -5 up to 0; 42 is inside the range and passes through.
  3. Safe empty case. max([], default=0) returns the default instead of raising ValueError.
  4. One argument must be iterable. max(5) tries to loop over 5 and raises TypeError.

Frequently Asked Questions

Call max(numbers). Use max(numbers, default=None) if the list might be empty.
Use numbers.index(max(numbers)), or max(range(len(numbers)), key=numbers.__getitem__) to avoid a second pass.
max(d, key=d.get) returns the key. max(d.values()) returns only the value.
The first maximal item encountered, in iteration order.
Yes. Strings compare character by character by Unicode code point, so max("apple", "banana") is "banana". Use key=str.lower for case-insensitive comparison.

Next: memoryview()

Zero-copy view of the memory of a bytes-like object.

memoryview() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful