In this article we will discuss detailed features of Python’s min() function with examples.

Python provides a min() function to find out the smallest element from a collection of elements,

  • Arguments:
    • Iterable : An Iterable object like list, tuple etc.
    • arg1, arg2 … : Multiple elements
    • key : A function that will be applied to each item in the Iterable and it returns a value based on the passed argument.
  • Returns:
    • It returns the element with minimum value in the Iterable or given elements. If key function is not provided then it directly compare the given items to find out the minimum value. If key function is provided then instead of comparing items directly it will call the key function on each item and then compare it with others.

Let’s see how to use the min() function.

Using min() function with an Iterable like list or tuple etc.

Find minimum value in a list using min()

Suppose we have a list of numbers i.e.

As list is an Iterable, so to find the minimum value in the list we can directly pass it to the min() function i.e.

Output:

Find the character with min ASCII value in a String using min()

Suppose we have a string i.e.

As String is an Iterable, so to find the character with minimum ASCII value in the string we can directly pass it to the min() function i.e.

Output:

min() function compared the characters in the string based on their ASCII value and returned the character with smallest ASCII value.

Find min string from list of Strings based on alphabetical order using min()

Suppose we have a list of strings i.e.

As list is an Iterable, so to find the minimum string based on alphabetical order in the list we can directly pass it to the min() function i.e.

Output:

Using min() function with Iterable & Custom comparater / key function

Till now we have seen examples where we are using min() function with default comparator i.e. it will use < operator to compare the elements while searching for min value. What if we want to find min element based on our custom logic or custom comparator.
Let’s see how to do that,

Find min length string from list of Strings using min()
Suppose we have a list of strings i.e.

Now we want to find the string with minimum size in this list of string. For that we need to pass the key argument to the min function i.e.

Output:

Find item in a dictionary with minimum value using min()

Output:

Some Important Points:

min function can find min element from similar type elements. If we pass different type elements then it will raise error min() will always will always return the first encountered minimum element. If there are multiple min elements then we need a customized solution to find all the min elements.

Using min() function with Multiple arguments

We can also pass the individual elements in min function instead of any Iterable i.e.

Output:

Complete example is as follows,

Output:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,