Python min()

Table of Contents

Introduction

Python provides the min() function to find the minimum (or smallest) element among a group of values.
This function is built-in and you can find it in the official documentation.

The Python min() function can be used with all iterators, therefore with lists, tuples and dictionaries.

In the next sections we will see in detail how to use the min() function with examples for each category.

Are you curious to find out how to find the largest element instead? Look at this tutorial!

Python min() function

If you have a list of values and you need to find the minimum of them, the min() function is the one for you!

Python provides the min() function to find the minimum (or smallest) value in an iterable or among a set of arguments.
In fact, there are two ways to use this function.

The first one is to pass an iterable that is a list, a tuple or a dictionary as an argument.

min(<my_iterable>)

Alternatively you can pass to the min() function directly the group of values you want to find the minum among.

min(<elem_1>, <elem_2>, <elem_3>, ... )

In both cases, this functions returns the minimum value of the iterable or arguments.

In the next sections we will see examples of use for both cases.

Python min() passing as argument an iterable

The first feature I want to show you is how to find the minimum (or smallest) value in an iterable.
I remind you that an iterable is a data structure such as a list, a tuple or a dictionary.

Let’s not waste any more time and see immediately how it works using some concrete examples.

Find the smallest item in a list

Let’s start from the simplest case, i.e. given a list of integers, we want to find the smallest element.

my_list = [1, 2, 4, 10, 3, 0]
print(min(my_list))

Output

0

As you can see, we have declared a list with only integers and thanks to the Python min() function we have found the smallest, i.e. 0.

Let’s complicate things a little and see what happens if we have strings instead of integers.Let’s complicate things a little and see what happens if we have strings instead of integers.
Also in this case, we declare a list but of strings.

my_list = ["aaaaaaaaaaaaa", "a", "zzzzz"]
print(min(my_list))

Output

'a'

As you can see from the example above, the Python min() function returns the smallest element ordered alphabetically.
Indeed, finding with strings it is possible to measure the length unlike integers which is more intuitive.

Find the smallest item in a tuple

Another category of iterables that I want to introduce you to is the tuple. Again it’s really easy to use the Python min() function.

Let’s start with a simple example, that is with a tuple of only integers.
Let’s take a look:

my_tuple = (1, 2, 4, 10, 3, 0)
print(min(my_tuple))

Output

0

As you can see it’s quite simple, it does nothing but return the smallest number in the tuple.

Obviously, as we have seen for lists, it is possible to create a tuple with only strings and return the smallest element to it.
Let’s see the example:

my_tuple = ["aaaaaaaaaaaaa", "a", "zzzzz"]
print(min(my_tuple))

As you can see also in this case the smallest alphabetically sorted string is returned.

Find the smallest item in a dictionary

Last category but not least are the dictionaries.
Well, even with this type of iterable it is possible to use the Python min() function, let’s see how.

First we must say that unlike lists and tuples, dictionaries are composed of keys and values.
For this reason, talking about the smallest element can be confusing. In fact you might wonder if the smallest element is between keys or between values.

Don’t worry, it’s possible to retrieve the smallest element in both keys and values.
Let’s start with the simplest case which is finding the smallest element among the keys.
Let’s take a look at the example:

my_dict = {1: "first_value", 3: "second_value", 0: "third_value"}
print(min(my_dict))

Output

0

As you can see, in this case the minimum value is taken from the keys of the dictionary and not the values.

As we said earlier, it is also possible to find the smallest element among values as well.
The only difference is that the operation must be broken into several parts.
First we need to find the key that has the smallest element in the dictionary as its value.
At this point it will be enough to access the dictionary with that key to obtain the smallest value.

But let’s go step by step. Let’s start with an example to see how to get the key whose value is the smallest element.

my_dict = {1: "first_value", 3: "second_value", 0: "third_value"}
print(min(my_dict, key = lambda k: my_dict[k]))

Output

# key with smallest value
1

As you can see, the Python min() function is called with an extra parameter this time.
This piece of lambda k: my_dict[k] code is used to parse all the key values in the dictionary.

At this point it is easy to find the smallest value by accessing the dictionary by key:

my_dict = {1: "first_value", 3: "second_value", 0: "third_value"}
# get key with smallest value
key_with_smallest_value = min(my_dict, key = lambda k: my_dict[k])
# get smallest value
print(my_dict[key_with_smallest_value])

Output

first_value

Possible exceptions

If you use the min() function with an empty iterable, a ValueError exception is raised.

my_list = []
min(my_list)

Output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: min() arg is an empty sequence

ValueError calling Python min() with empty sequence
ValueError calling Python min() with empty sequence

Python min() passing multiple arguments

It is possible to use the min() function without integrable but directly passing the elements as parameters.
Here are a couple of examples of use with integers, floats and strings.

# integers
print(min(2, 3, 6, 0, 4))
# floats
print(min(3.7, 3.0, 0.6, 3.33333, 2.56))
# strings
print(min("hello", "how", "are", "you"))

Output

0
0.6
'are'

Possible exceptions

Also in this case, if you pass an empty iterable to the Python min() function, a ValueError exception is raised.

min([])

Output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: min() arg is an empty sequence

Another possible exception occurs when you don’t pass any parameters to the Python min() function.
Let’s see the example.

min()

Output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: min expected 1 argument, got 0

In this case, a TypeError exception is raised instead

TypeError calling Python min() without any sequence
TypeError calling Python min() without any sequence

Conclusion

Here we are at the end of this tutorial.
As always I hope you are clear and now you know how to best use the min() function in Python.
If you have any doubts or errors, do not hesitate to let me know in the comments.

Otherwise, if everything is clear to you, keep learning and move on to the next article!

Leave a Comment

Your email address will not be published. Required fields are marked *