🚀 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...

min()

AI & DATA SCIENCE // min

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

Syntax

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

Deep Dive Course

min() mirrors max() exactly, but for the smallest value: pass a single iterable or several separate arguments, an optional key function to change what 'smallest' means, and an optional default that's returned instead of raising ValueError when an iterable happens to be empty.

1Understanding min()

min() mirrors max() exactly, but for the smallest value: pass a single iterable or several separate arguments, an optional key function to change what 'smallest' means, and an optional default that's returned instead of raising ValueError when an iterable happens to be empty.

💡

min() and max() both resolve ties by returning the FIRST item that achieves the extreme value, which matters when several items compare equal under a key function.

editor.html
temperatures = [72, 68, 75, 61, 80]
print(min(temperatures))
localhost:3000

2Practical Example

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

editor.html
flights = [{"airline": "A", "price": 320}, {"airline": "B", "price": 275}]
cheapest = min(flights, key=lambda f: f["price"])
print(cheapest["airline"])
localhost:3000

3Best Practices

Follow these guidelines when working with min():

1. Use key= to find the item with the smallest computed property instead of manually tracking a running minimum in a loop

2. Pass default=... for iterables that might be empty at runtime

3. Combine min() and max() to compactly compute a range from a single collection

⚠️

Tip: min() and max() both resolve ties by returning the FIRST item that achieves the extreme value, which matters when several items compare equal under a key function.

editor.html
temperatures = [72, 68, 75, 61, 80]
print(min(temperatures))
localhost:3000

Examples

Example 01Basic Usage
temperatures = [72, 68, 75, 61, 80]
print(min(temperatures))
Example 02Advanced Example
flights = [{"airline": "A", "price": 320}, {"airline": "B", "price": 275}]
cheapest = min(flights, key=lambda f: f["price"])
print(cheapest["airline"])

Best Practices

  • Use key= to find the item with the smallest computed property instead of manually tracking a running minimum in a loop
  • Pass default=... for iterables that might be empty at runtime
  • Combine min() and max() to compactly compute a range from a single collection

Interview Question

If two items tie for the smallest value under a key function, which one does min() return?

Hint: Think about how min() scans the iterable.

min() scans left to right and only replaces its current best when it finds something strictly smaller, so on a tie it keeps whichever tied item it saw first. The same first-wins rule applies to max() for ties on the largest value.

Exercises

MediumPractice using min() in a real scenario.
View Solution
temperatures = [72, 68, 75, 61, 80]
print(min(temperatures))

Frequently Asked Questions

If two items tie for the smallest value under a key function, which one does min() return?

min() scans left to right and only replaces its current best when it finds something strictly smaller, so on a tie it keeps whichever tied item it saw first. The same first-wins rule applies to max() for ties on the largest value.

Related Functions

max()sorted()key parameter