Python : min() function Tutorial with examples

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,

min(iterable, *[, key, default])
min(arg1, arg2, *args[, key])
  • 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.

# List of numbers
listOfNum = [22, 34, 11, 33, 10, 35, 19, 33, 9, 10]

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.

# Find minimum value in a list
minValue = min(listOfNum)

print('minimum value in List : ', minValue)

Output:

minimum value in List :  9

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

Suppose we have a string i.e.

sampleStr = 'thisissamplestring'

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.

# Find character with min ASCII value in string
minValue = min(sampleStr)

print('Character with min ASCII value : ', minValue)

Output:

Character with min ASCII value :  a

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.

listOfStr = ['hi', 'this', 'is', 'a', 'small', 'string', 'with', 'msg']

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.

# Get string with min value in list of string based on alphabetical
minValue = min(listOfStr)

print(minValue)

Output:

a

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.

listOfStr = ['hi', 'this', 'is', 'a', 'small', 'text', 'with', 'msg']

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.

# Get the string with minimum size in this list of string
minValue = min(listOfStr, key=lambda x: len(x))

print('min Length String in List of Strings : ', minValue)

Output:

min Length String in List of Strings :  a

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

sampleDict = {'Ritika': 5, 'Sam': 27, 'John': 10, 'Sachin': 14, 'Mark': 19}

# Get Item with min value in dictionary
minValue = min(sampleDict.items(), key=lambda x: x[1])

print('Item with min Value in Dictionary : ', minValue)

Output:

Item with min Value in Dictionary :  ('Ritika', 5)

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.

# Find minimum value in a given numbers
minValue = min(22, 34, 11, 33, 10, 35, 19, 33, 9, 10)

print('minimum value : ', minValue)

Output:

minimum value :  9

Complete example is as follows,

def main():
    print('*** Find minimum value in a list using min() ***')

    # List of numbers
    listOfNum = [22, 34, 11, 33, 10, 35, 19, 33, 9, 10]

    print('List Contents : ', listOfNum)

    # Find minimum value in a list
    minValue = min(listOfNum)

    print('minimum value in List : ', minValue)

    print('*** Find the character with min value in a String using min() ***')

    sampleStr = 'this is sample string'

    # Find character with min ASCII value in string
    minValue = min(sampleStr)

    print('Character with min ASCII value : ', minValue)

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

    listOfStr = ['hi', 'this', 'is', 'a', 'small', 'string', 'with', 'msg']

    # Get string with min value in list of string based on alphabetical
    minValue = min(listOfStr)

    print(minValue)

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

    print('** Find min length string from list of Strings using min() **')

    listOfStr = ['hi', 'this', 'is', 'a', 'small', 'text', 'with', 'msg']

    # Get the string with minimum size in this list of string
    minValue = min(listOfStr, key=lambda x: len(x))

    print('min Length String in List of Strings : ', minValue)

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

    sampleDict = {'Ritika': 5, 'Sam': 27, 'John': 10, 'Sachin': 14, 'Mark': 19}

    # Get Item with min value in dictionary
    minValue = min(sampleDict.items(), key=lambda x: x[1])

    print('Item with min Value in Dictionary : ', minValue)

    print('*** Using min() function with Multiple arguments ***')

    # Find minimum value in a given numbers
    minValue = min(22, 34, 11, 33, 10, 35, 19, 33, 9, 10)

    print('minimum value : ', minValue)

if __name__ == '__main__':
    main()

Output:

*** Find minimum value in a list using min() ***
List Contents :  [22, 34, 11, 33, 10, 35, 19, 33, 9, 10]
minimum value in List :  9
*** Find the character with min value in a String using min() ***
Character with min ASCII value :  a
*** Find min string from list of Strings based on alphabetical order using min() ***
a
*** Using min() function with Iterable & Custom comparater / key function ***
** Find min length string from list of Strings using min() **
min Length String in List of Strings :  a
** Find item in a dictionary with minimum value using min() **
Item with min Value in Dictionary :  ('Ritika', 5)
*** Using min() function with Multiple arguments ***
minimum value :  9

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top