Python min() Function

Click to share! ⬇️

Python min() function

The Python min() function is another useful function for working with numerical data in your Python programs. The min() function can accept a sequence as its input or a series of two or more items as input. If you pass a sequence to the min() function, it returns the smallest item in that sequence. If you instead pass two or more items, the min() function will return the smallest item of the items you passed in. Let’s look at some examples of how you might use the Python min() function now.

Python min() Function Example 1

The first thing we can do is to initialize a sequence in order to test the min() function. In this case, it is a list of the integers 12, -3, 17, -5, 99, and 0.

list of integers

Now we can call the min() function and pass in the list_of_nums variable to see what the smallest integer is, which in this case is -5.

min function list

Python min() Function Example 2

The same approach can be used on a Tuple when using the min() function. In this second example, we again initialize the sequence to contain the same sequence of numbers.

tuple of integers

Using the min() function once again, now on the Tuple, produces the same result with -5 being the smallest number.

min function tuple

Python min() Function Example 3

Now we can test the min() function on some floating-point numbers. Again, we’ll first test this on a list of floating-point numbers containing the values 1.8, -26.13, -9.53, 105.8, and -6.123.

list of floating point numbers

When min() is applied to this list of floating-point numbers, we find -26.13 is the smallest value.

min floating point

Python min() Function Example 4

In this example, we simply pass in a series of numbers to see the result of the min() function.

min series numbers

Python min() Function Example 5

The min() function can also be used with a Python Dictionary. When you use min() on a dictionary, it looks at the keys to determine the smallest value.

min dict

In the snippet just above, the keys of the dictionary used integer values. This next snippet reverses that and uses strings as the keys and integers as the values. Once again we call min() on this dictionary, and ‘a’ is the smallest value.

min dict string key

Setting A Default Value

If you pass an empty iterable to the min() function, you’ll get an error of ValueError: min() arg is an empty sequence.

Traceback most recent call last min value error

You can handle this situation by providing a default value. This way, if there is no minimum value, the default is returned.

min function default value

Using A Custom Key

You can use a custom key with the min() function. You might use this if you want to find the minimum length in characters of a string, rather than the minimum value of a string based on a through z.

min using key

Python min() Function Summary

The Python min() function returns the item with the lowest value or the item with the lowest value in an iterable. If the values are strings, the comparison is done alphabetically, however, we saw a way to change that by using a custom key. min() accepts one or more items to compare or an iterable with one or more items to compare. We also saw how to set a default value just in case an empty iterable is passed into the function.

Click to share! ⬇️