October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Find the Maximum Value in Python Using max()

Use Python’s built-in max() to find the largest item in an iterable, rank records with key, handle empty inputs with default, or identify the dictionary key mapped to the greatest value.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Python’s built-in max() to find the largest item in a list or other iterable: max([3, 5, 9, 1]) returns 9. For dictionary values or records, pass a key function that tells Python what to compare.

Find the largest item in a list or iterable

Pass a single iterable—such as a list, tuple, or other sequence—to max():

numbers = [3, 5, 9, 1]
largest = max(numbers)
print(largest)  # 9

Python’s built-in function returns the largest item in an iterable, or the largest of two or more arguments. See the official built-in functions documentation.

Choose the right call form

One positional argument is treated as an iterable. Two or more positional arguments are compared directly; they are not treated as one collection.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
max([3, 5, 9, 1])  # 9: one iterable
max(3, 5, 9, 1)    # 9: several arguments

The documented forms are max(iterable, *, key=None), max(iterable, *, default, key=None), and max(arg1, arg2, *args, key=None). The default argument applies only to the single-iterable form and must be passed by keyword.

Handle an empty iterable

Calling max() on an empty iterable without a fallback raises ValueError. If emptiness is expected, provide a meaningful default:

largest = max([], default=0)
print(largest)  # 0

Choose a fallback that cannot be mistaken for a real maximum in your application. For example, if zero is a valid value and you need to distinguish an empty input from a maximum of zero, use a separate sentinel or check for emptiness instead of returning zero.

The default parameter was added in Python 3.4. The official documentation also notes that allowing key=None was added in Python 3.8.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Find the longest word or rank items with key

Use key when you want to compare items by a derived property. The function receives each item and returns the value used for comparison; max() returns the original item, not the key result.

words = ["pear", "watermelon", "fig"]
longest = max(words, key=len)
print(longest)  # watermelon

Here, Python compares word lengths but returns the matching word. The same pattern works for objects or records when you supply a function that extracts the attribute you want to rank.

Get the dictionary key with the largest value

Iterating over a dictionary visits its keys. To find the key whose mapped value is greatest, use the dictionary’s get method as the comparison key:

scores = {"Mina": 88, "Leo": 94, "Aria": 91}
top_scorer = max(scores, key=scores.get)
print(top_scorer)  # Leo

This returns "Leo", the key—not the score. To retrieve both the key and value as a pair, compare the dictionary’s items by their second element:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
top_entry = max(scores.items(), key=lambda item: item[1])
print(top_entry)  # ('Leo', 94)
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Know how ties are resolved

If multiple items share the maximum comparison value, max() returns the first one encountered. For example, with equal scores, the first matching dictionary entry in iteration order wins. If your application needs a different tie-breaker, encode it in the key function or identify all tied items explicitly.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.