Home Python Built-in Functions min() Python min() Function Definition In Python, min() returns the smallest item. Call it with one iterable, min([4, 2, 9]), or with two or more separate arguments, min(4, 2, 9).
The optional key function decides what to compare, so you can find the cheapest product or the shortest word while still getting the original item back. If several items tie, the first one is returned. An empty iterable raises ValueError unless you pass default.
min() is a built-in function : it lives in the builtins module, so it is always available without an import.
Syntax Give it an iterable or several values; add key or default by keyword.
smallest = min(iterable, key=None)
smallest = min(a, b, c)
smallest = min(iterable, default=value) Call What it does min(4, 9, 2)Returns 2. min("python")Returns "h", the character with the lowest code point. min(words, key=len)Returns the shortest word. min([], default=0)Returns 0 instead of raising.
Strings compare by Unicode code point, so uppercase letters sort before lowercase: min("b", "B") is "B".
Library declarations min(iterable, *, key=None)
min(iterable, *, default, key=None)
min(arg1, arg2, *args, key=None) Parameters Parameter Description iterable An iterable of comparable items (one-argument form). arg1, arg2, *args Two or more values to compare (multi-argument form). key Optional, keyword-only. A one-argument function whose result is compared, such as len or str.lower. default Optional, keyword-only, iterable form only. Returned when the iterable is empty.
Return Value Return Value Description item The smallest item itself (not its key value). The first one wins on ties.
Important
min([]) raises ValueError; pass default= when the data may be empty. Mixing types that cannot be compared, such as min(3, "a"), raises TypeError.
Python Examples 1. Numbers and text 2. Using key 3. Empty data Example 1 — Smallest Number, Character, and Word Call min() with separate arguments, a list of floats, a string, and a list of words.
print(min(4, 9, 2, 7))
print(min([3.5, -1.0, 2.25]))
print(min("python"))
print(min(["pear", "Apple", "fig"])) How It Works Separate arguments. min(4, 9, 2, 7) compares the four values and returns 2.Any iterable. A list of floats works the same way; the smallest is -1.0.Characters by code point. A string is iterable, and "h" has the lowest code point of the letters in "python".Capitals first. "A" (65) is lower than any lowercase letter, so "Apple" beats "fig".Example 2 — Cheapest Product and Shortest Word with key Pass a key function to compare one part of each item while returning the whole item.
products = [("laptop", 899), ("mouse", 25), ("monitor", 199), ("cable", 25)]
cheapest = min(products, key=lambda p: p[1])
shortest = min(["banana", "kiwi", "cherry"], key=len)
lowest_ci = min(["pear", "Apple", "fig"], key=str.lower)
print("Cheapest:", cheapest)
print("Shortest word:", shortest)
print("First alphabetically (ignoring case):", lowest_ci) Cheapest: ('mouse', 25)
Shortest word: kiwi
First alphabetically (ignoring case): Apple How It Works Compare the price. key=lambda p: p[1] makes min() look only at the second element of each tuple.Ties keep order. mouse and cable both cost 25; the first one in the list, mouse, is returned.Built-in key functions. key=len compares word lengths, so "kiwi" (4 letters) wins.Case-insensitive. key=str.lower compares lowercase copies but returns the original "Apple".Example 3 — Empty Lists, default, and a Dict's Lowest Value Handle empty input safely and find the dictionary key that has the smallest value.
empty = []
print(min(empty, default=None))
print(min(empty, default="no data"))
try:
min(empty)
except ValueError:
print("min() of an empty list raises ValueError")
temperatures = {"Mon": 21, "Tue": 17, "Wed": 19}
coldest_day = min(temperatures, key=temperatures.get)
print(coldest_day, temperatures[coldest_day]) None
no data
min() of an empty list raises ValueError
Tue 17 How It Works default for empty input. With nothing to compare, min() returns the default value you supply.No default, no answer. Without default, an empty list raises ValueError.Iterating a dict gives keys. min(temperatures) alone would compare the day names alphabetically.key=dict.get. Comparing temperatures.get(day) finds the day with the lowest reading: Tue at 17.Frequently Asked Questions How do I find the smallest number in a list? Call min(numbers). For an empty list pass a fallback: min(numbers, default=0).
What does the key argument do in min()? It is a function applied to each item before comparing. min(words, key=len) compares lengths but returns the word itself.
What happens if two items are equally small? min() returns the first one it found, in the order of the iterable.
How do I find the key with the smallest value in a dict? Use min(d, key=d.get). To get both, use min(d.items(), key=lambda kv: kv[1]).
Why does min("b", "B") return "B"? Strings compare by Unicode code point, and uppercase letters have lower code points than lowercase ones. Use key=str.lower to ignore case.
Next: next() Get the next item from an iterator, with an optional default.
next() → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion