## 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 the official documentation here.

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

Are you curious to find out how to find the larger 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.

#### List

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

**Output**

`0`

And what about a list of strings?

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

**Output**

`'a'`

In this case the min() function returns the smallest element ordered alphabetically.

#### Tuple

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

**Output**

`0`

#### Dictionary

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

It is also possible to search which key has the smallest value using this code:

```
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
```

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 without any iterable, 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
```

### 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

If you use the **min()** function without any argument, a **TypeError** exception is raised.

`min()`

**Output**

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

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