How to find the maximum value in a list in Python

· 1 min read · Updated March 17, 2026 · beginner
python lists beginner

The simplest way is Python’s built-in max() function.

Using max()

numbers = [3, 7, 2, 9, 4]
maximum = max(numbers)
print(maximum)  # 9

One line, no imports needed.

Using max() with a key

Find the longest string in a list:

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

Using max() with empty list

Provide a default value to avoid a ValueError on empty lists:

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

Using a loop

numbers = [3, 7, 2, 9, 4]
maximum = numbers[0]

for num in numbers[1:]:
    if num > maximum:
        maximum = num

print(maximum)  # 9

Useful when you need more control over the comparison logic.

Common mistake

Don’t compare directly in a loop without initializing:

numbers = [3, 7, 2, 9, 4]
for num in numbers:
    if num > maximum:  # NameError: name 'maximum' is not defined
        maximum = num

Always initialize with the first element or use max().

See Also