Python max() Function Explained

The built-in max() function returns the largest item.

You can use it in two main ways:

  • To get the largest item from an iterable like a list or tuple
  • To compare two or more separate values

This is useful when you want the highest number, the alphabetically largest string, or the biggest item based on a custom rule.

Quick example

numbers = [3, 8, 2, 5]
print(max(numbers))

print(max(3, 8, 2, 5))

words = ["apple", "banana", "pear"]
print(max(words))

Output:

8
8
pear

In this example:

  • max(numbers) uses the iterable form
  • max(3, 8, 2, 5) uses separate arguments
  • max(words) compares strings alphabetically

What max() does

max():

  • Returns the largest item
  • Works with a list, tuple, string, set, or other iterable
  • Also works with two or more separate values
  • Compares items using Python’s normal ordering rules

For numbers, “largest” means the highest value.

For strings, “largest” means the item that comes last in alphabetical order.

Basic syntax

Here are the main forms of max():

max(iterable)
max(iterable, *, key=func)
max(iterable, *, default=value)
max(arg1, arg2, *args)
max(arg1, arg2, *args, key=func)

The most important parts are:

  • iterable: a collection like a list, tuple, or string
  • key: a function that tells Python how to compare items
  • default: a value to return if the iterable is empty

Using max() with a list of numbers

This is the most common beginner use case.

scores = [72, 91, 85, 66]
highest_score = max(scores)

print(highest_score)

Output:

91

This works well for:

  • Scores
  • Prices
  • Ages
  • Counts

Another example:

prices = [19.99, 5.50, 12.75, 29.00]
print(max(prices))

Output:

29.0

If you only need the largest value, max() is simpler than sorted().

Using max() with separate values

You do not always need a list. You can pass values directly.

print(max(4, 12, 7))

Output:

12

This is useful for quick comparisons.

For example, comparing three numbers entered by a user:

a = 15
b = 42
c = 27

largest = max(a, b, c)
print(largest)

Output:

42

Use this form when you already have separate values and do not need to store them in a list first.

Using max() with strings

max() can compare strings too.

words = ["apple", "banana", "pear"]
print(max(words))

Output:

pear

This happens because strings are compared alphabetically.

A few important points:

  • "pear" comes after "banana" alphabetically
  • Uppercase and lowercase letters can change the result
  • Mixed text case can surprise beginners

Example:

words = ["apple", "Banana", "pear"]
print(max(words))

The result may not be what you expect because uppercase letters are compared differently from lowercase letters.

You can fix that with the key argument, which is explained below.

Using the key argument

The key argument lets you control how items are compared.

This is very useful when the “largest” item is not simply the normal largest value.

Find the longest word

words = ["cat", "elephant", "dog"]
print(max(words, key=len))

Output:

elephant

Here, key=len tells Python to compare the lengths of the strings instead of the strings themselves.

If you are new to len(), see len() explained.

Compare strings without caring about uppercase or lowercase

words = ["apple", "Banana", "pear"]
print(max(words, key=str.lower))

Output:

pear

Here, str.lower converts each string to lowercase for comparison.

Use a lambda function

You can also use a small custom function with lambda:

items = ["a", "bb", "cccc", "ddd"]
print(max(items, key=lambda text: len(text)))

Output:

cccc

For beginners, key=len and key=str.lower are the most useful examples to remember.

Using the default argument

The default argument only works with the iterable form.

It prevents an error when the iterable is empty.

numbers = []
print(max(numbers, default=0))

Output:

0

This is helpful when a list may have no items.

Without default, this would raise an error.

Another example:

words = []
result = max(words, default="no words found")
print(result)

Output:

no words found

Important:

  • default works with max(iterable, default=value)
  • It does not work with multiple separate arguments like max(1, 2, 3, default=0)

What errors can happen

There are a few common errors when using max().

ValueError with an empty iterable

If the iterable is empty and you do not provide default, Python raises a ValueError.

numbers = []
print(max(numbers))

Error:

ValueError: max() arg is an empty sequence

Fix it by using default:

numbers = []
print(max(numbers, default=None))

If you need more help with this kind of problem, see ValueError in Python: causes and fixes.

TypeError when items cannot be compared

If Python cannot compare the items, you get a TypeError.

items = [10, "20", 30]
print(max(items))

This fails because integers and strings are different types and cannot be compared normally.

Fix it by making the items the same type:

items = [10, 20, 30]
print(max(items))

If you are debugging this kind of issue, see TypeError in Python: causes and fixes.

TypeError when called with no arguments

max() needs at least one iterable or at least two values.

print(max())

This raises a TypeError.

max() vs sorted()

These two functions are related, but they do different things.

max():

  • Returns one largest item

sorted():

  • Returns a new sorted list of all items

Example:

numbers = [3, 8, 2, 5]

print(max(numbers))
print(sorted(numbers))

Output:

8
[2, 3, 5, 8]

Use max() when you only need the largest value.

Use sorted() when you need all items in order.

If you want the smallest item instead, compare this with min().

Common mistakes

Beginners often run into these problems:

  • Calling max() on an empty list without default
  • Mixing incompatible types like integers and strings
  • Expecting max() to return all large items instead of one item
  • Forgetting that strings are compared alphabetically
  • Using default with multiple separate arguments

If something is not working, these quick checks can help:

print(items)
print(type(items))
print(len(items))
print(max(items))
print(max(items, default=None))
print([type(x) for x in items])

These are useful for checking:

  • What is actually inside items
  • Whether items is a list or some other type
  • Whether the iterable is empty
  • Whether all items have compatible types

Be careful with print(max(items)) if items might be empty or contain mixed types, because that line can also raise an error.

FAQ

What does max() return in Python?

It returns the largest item from an iterable or the largest of two or more values.

Can max() work with strings?

Yes. It compares strings using alphabetical order, based on character values.

What happens if the list is empty?

max() raises a ValueError unless you use the default argument with the iterable form.

How do I get the longest string with max()?

Use key=len:

words = ["cat", "elephant", "dog"]
print(max(words, key=len))

What is the difference between max() and sorted()?

max() returns one largest item. sorted() returns all items in order.

See also