🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

max()

AI & DATA SCIENCE // max

max() returns the largest item in an iterable, or the largest of several arguments passed directly.

Syntax

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

Deep Dive Course

max() can be called two ways: with a single iterable, or with two or more separate arguments. The optional key function lets you rank items by something other than their natural ordering — for example, finding the longest string in a list instead of the alphabetically last one. default supplies a fallback value instead of raising ValueError when the iterable is empty.

1Understanding max()

max() can be called two ways: with a single iterable, or with two or more separate arguments. The optional key function lets you rank items by something other than their natural ordering — for example, finding the longest string in a list instead of the alphabetically last one. default supplies a fallback value instead of raising ValueError when the iterable is empty.

💡

Pass default=... to max() to avoid a crash when the iterable might be empty — always consider whether that's possible.

editor.html
scores = [88, 95, 72, 100, 61]
print(max(scores))
localhost:3000

2Practical Example

Here is a real-world application of max() showing how it is used in production Python code.

editor.html
students = [{"name": "Ana", "score": 91}, {"name": "Boris", "score": 87}]
top = max(students, key=lambda s: s["score"])
print(top["name"])
localhost:3000

3Best Practices

Follow these guidelines when working with max():

1. Use the key= parameter instead of manually looping to find the item with the largest computed property

2. Pass default=... when the iterable could plausibly be empty, to avoid a crash

3. Use max(a, b) for two plain values instead of an if/else comparison

⚠️

Tip: Pass default=... to max() to avoid a crash when the iterable might be empty — always consider whether that's possible.

editor.html
scores = [88, 95, 72, 100, 61]
print(max(scores))
localhost:3000

Examples

Example 01Basic Usage
scores = [88, 95, 72, 100, 61]
print(max(scores))
Example 02Advanced Example
students = [{"name": "Ana", "score": 91}, {"name": "Boris", "score": 87}]
top = max(students, key=lambda s: s["score"])
print(top["name"])

Best Practices

  • Use the key= parameter instead of manually looping to find the item with the largest computed property
  • Pass default=... when the iterable could plausibly be empty, to avoid a crash
  • Use max(a, b) for two plain values instead of an if/else comparison

Interview Question

How do you use max() to find the dictionary with the highest value for a specific key, in a list of dictionaries?

Hint: The key parameter accepts a function, not just an attribute name.

Pass a key function that extracts the value you want to compare, such as a lambda that pulls out a particular field from each dictionary. max() calls that function on every element, compares the results, and returns the original dictionary that produced the largest value, not the value itself.

Exercises

MediumPractice using max() in a real scenario.
View Solution
scores = [88, 95, 72, 100, 61]
print(max(scores))

Frequently Asked Questions

How do you use max() to find the dictionary with the highest value for a specific key, in a list of dictionaries?

Pass a key function that extracts the value you want to compare, such as a lambda that pulls out a particular field from each dictionary. max() calls that function on every element, compares the results, and returns the original dictionary that produced the largest value, not the value itself.

Related Functions

min()sorted()key parameter