Note

Python - min() - Find the smallest value


Overview of the Python min() function. Find the smallest item in the iterable.

min(iterable_1, ..., key=func, default=obj) returns the smallest item from a single iterable or the smallest of two or more arguments.

Accepts optional key-word key argument that needs to be a function that will be used to evaluate each item's number/value.

With two or more arguments provided, min() will return the smallest one. If two elements or arguments have the same value min() will return the first one found.

min() also accepts optional key-word default argument that will be returned if provided iterable is empty.


my_list = [11, 0.2, 77]

my_min = min(my_list)

print(my_min)  # 0.2

Output:

0.2

Using min() function with string values

my_list = ["www", "devcuriosity", "com"]

my_min = min(my_list)

print(my_min)  # com
print(f'ord("w") = {ord("w")}, ord("d") = {ord("d")}, ord("c") = {ord("c")}')  # ord("w") = 119, ord("d") = 100, ord("c") = 99

Output:

com
ord("w") = 119, ord("d") = 100, ord("c") = 99

This one may be a bit counter-intuitive. In this example min() is taking into account only the first letter of each word and is calculating min based on ASCII value of a given letter. In this example, with function ord() we can see that letter "c" has the smallest value - 99.


Using min() function with key argument provided

my_list = ["www", "devcuriosity", "com"]

my_min = min(my_list, key=len)

print(my_min)  # www

Output:

www

Here everything looks like we would expect, thanks to key-word argument key. We provided len as a function that should be used to evaluate each argument value. So in this example, we told min() to simply check for the shortest word in a list.


Using min() with multiple iterables (without key argument)

my_list_1 = [2, 50, 777]
my_list_2 = [80, 3]

my_min = min(my_list_1, my_list_2)

print(my_min)  # [2, 50, 777]

Output:

[2, 50, 777]

Here, we provided not one, but two iterables to min(). And without a specific function, min() checked that 2 is smaller than 80, so it returned [2, 50, 777].


Using min() with multiple iterables (with provided key argument)

my_list_1 = [2, 50, 777]
my_list_2 = [80, 3]

my_min = min(my_list_1, my_list_2, key=lambda x: sum(x))

print(my_min)  # [80, 3]

Output:

[80, 3]

In this example, we also provided two iterables, but we also specified key argument with function sum() to evaluate our lists based on their combined value.


Using min() with default on an empty iterable

my_list = []

my_min = min(my_list, default="Nothing here")

print(my_min)  # Nothing here

Output:

Nothing here

And here we can see optional key-word argument default in action. When default is not provided, calling min() on an empty iterable raises a ValueError.


Key Points to Remember about min()

  • Versatile with Iterables and Arguments: min() can find the smallest item in an iterable or among multiple arguments.

  • Custom Evaluation with key: The key argument enables custom criteria for determining the "smallest" item.

  • Handling Empty Iterables: The default argument provides a fallback for empty iterables, avoiding ValueError.

  • First Occurrence Preference: If multiple items have the same key value, min() returns the first one encountered.

I hope you found this helpful! If you have any feedback, questions or requests, please reach out to me through Contact page!

Thank You Image


You might also be interested in: