min()

The smallest item of an iterable — max’s mirror, same options, same traps.

Built-in functionPython 1.0+Live demo
Common call
min(prices)
Returns
the item itself, not its index
Replaces
strings compare lexicographically: min(['10', '2']) is '10'
Watch out
empty iterable raises ValueError — pass default= to survive it
min(iterableiterable — The items to compare. Alternatively pass two or more positional arguments.type: iterable · required, *, keykey — One-argument function computing the comparison key, e.g. key=len.type: callable · default: None=None, defaultdefault — Returned when the iterable is empty. Without it, empty raises ValueError.type: Any · default: (none)=...)
→ Any

Demo

Live evaluation
Try:
Inputs
itemslistcomma-separated items
Output
min(['banana', 'apple', 'cherry'])
'apple'

Items are strings here, so comparison is lexicographic — ’10’ beats ’2’ because ’1’ < ’2’. Use key=int in real code for numeric strings. Empty input raises Python’s exact error.

Parameters

NameTypeRequiredDescription
iterableiterableyesThe items to compare. Alternatively pass two or more positional arguments.
keycallableno (None)One-argument function computing the comparison key, e.g. key=len.
defaultAnyno ((none))Returned when the iterable is empty. Without it, empty raises ValueError.

Return value

Any — The smallest item. Empty iterable raises ValueError unless default is given. Also callable as min(a, b, ...).

Common patterns

Smallest by a computed key
argmin behavior — the ITEM with the smallest key.
cheapest = min(products, key=lambda p: p.price)
Safe min of possibly-empty data
default turns the empty-sequence error into a value.
lowest = min(scores, default=0)
Clamping a value
min caps the top, max lifts the bottom.
clamped = max(lo, min(x, hi))

Examples

1. Min of an iterable
min([3, 1, 4, 1, 5])
Returns
1
2. Strings — lexicographic
min(["10", "9", "2"])
Returns
'10'
3. Two-argument form
min(3, 7)
Returns
3
4. Empty with default
min([], default=0)
Returns
0

Pitfalls

1. Empty iterable raises
Same trap as max — pass default when data can be empty.
Raises
min([])
ValueError: min() arg is an empty sequence
Fix
min([], default=0)
0
2. Digit strings compare as text
"10" < "2" lexicographically.
Surprising
min(["10", "9"])
'10'
Numeric
min(["10", "9"], key=int)
'9'

When to use

Use it
  • Smallest item, or item with the smallest key (argmin)
  • Pairwise comparisons via the multi-argument form
  • Clamping combined with max
Reach for something else
  • Bottom-k items → heapq.nsmallest
  • Running minimum in a loop → track it yourself

Notes

Complexity
O(n) — single pass
Return
an item from the input (not a copy)
CPython impl
Python/bltinmodule.c :: builtin_min
Memory
No allocation
Thread-safe
Yes for the scan; the source should not mutate concurrently

FAQ

There is no built-in; a manual loop or sorted()[0] / [-1] (two passes hidden in one sort) are the options. For big data, loop once.

History

3.4
The default keyword argument added.
2.5
The key keyword argument added.