Skip to main content

How to Find the Largest Item in a List in Python with max()

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

The hottest day of the week, the highest score, the biggest file: max() looks at the values you give it and hands back the largest one. This guide finds the highest of four temperatures on Python 3.15.2.

Syntax​

Syntax
<largest> = max(<items>)
<largest> = max(<first>, <second>)
PartWhat it means
<items>A list, or another group of values, to look through
<first>, <second>Two or more separate values, instead of one list
<largest>The largest of them

The Python documentation says max will "Return the largest item in an iterable or the largest of two or more arguments." An iterable is anything Python can go through item by item, such as a list.

The Example​

A list holds the temperatures of four days, in degrees: 18, 24, 21 and 19. The script prints the highest one. Then it prints the larger of two separate numbers, 3 and 7.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

temperatures.py
temperatures = [18, 24, 21, 19]

print(max(temperatures))
print(max(3, 7))

The temperatures.py script in a macOS-style code window: a temperatures list of 18, 24, 21 and 19, then prints of max(temperatures) and max(3, 7).

Step 2: Run It​

Terminal
uv run python temperatures.py
Output
24
7

The terminal after uv run python temperatures.py: 24, then 7.

24 is the highest of the four temperatures, wherever it stands in the list. max(3, 7) compared its two values and gave the larger one, 7. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Passing an empty listmax([]) raised ValueError: max() iterable argument is emptyGive a value to use when the list is empty: max([], default=0) gave 0
Passing a single numbermax(5) raised TypeError: 'int' object is not iterablePut it in a list, max([5]) gave 5, or pass two or more values
Mixing text and numbersmax([18, "24", 21]) raised TypeError: '>' not supported between instances of 'str' and 'int'Turn every item into a number first
Comparing numbers stored as textmax(["9", "24", "100"]) gave '9', because text is compared character by characterCompare them as numbers: max(["9", "24", "100"], key=int) gave '100'

FAQ​

How do I find the longest word in a list?​

Pass key=len, so max compares the lengths instead of the words. max(["pear", "apple", "fig"], key=len) gave 'apple'. The documentation says: "The key argument specifies a one-argument ordering function like that used for list.sort()."

What if two items are equally large?​

You get the first one. max(["pear", "plum", "fig"], key=len) gave 'pear', although plum is just as long. The documentation says: "If multiple items are maximal, the function returns the first one encountered."

How do I find the position of the largest item?​

Ask the list where that value is: temperatures.index(max(temperatures)) gave 1, the second place, because positions start at 0.

What does max() do with words?​

It gives the one that comes last in alphabetical order: max(["pear", "apple", "fig"]) gave 'pear'. Capital letters come before small ones, so max(["apple", "Zebra"]) gave 'apple'. To ignore capitals, compare the lowercase forms: with key=str.lower the same call gave 'Zebra'.

What does max() do with a dictionary?​

It compares the keys. With days = {"mon": 18, "tue": 24, "wed": 21}, max(days) gave 'wed', the last key in alphabetical order. For the largest value, use max(days.values()), which gave 24. For the key that holds the largest value, max(days, key=days.get) gave 'tue'.

Conclusion​

max(temperatures) went through the four temperatures and gave 24, and max(3, 7) gave 7. Give max one list or several separate values, and add default when the list can be empty.

Additional Resources​

  • max() — the function, with key and default.
  • Comparisons — which values can be compared with each other.