Cheap Jerseys Cheap NFL Jerseys Cheap NFL Jerseys Cheap Football jerseys Cheap NFL Jerseys Wholesale Jerseys Cheap NHL Jerseys Cheap Jerseys From China Cheap MLB Jerseys Cheap Jerseys Cheap Jerseys Cheap Jerseys Wholesale Jerseys Python min() Function - WTMatter

Python min() Function

Python Min() Function

This is a tutorial of the Python built-in min() Function. Learn to find the smallest item either in a given iterable or among the multiple arguments.

Python min() Function

The function min() returns the smallest item present in a given iterable object. It can also be used to find the smallest value among multiple arguments that are passed to this function.

In other words, you can use the min() function to find the smallest item in two different ways. You can either provide the iterable as the only argument to this function to find out the item with the smallest value or you can also provide the items directly as the arguments to this function to check which is the smallest value among them.

Note. max() is the opposite function of the min() function.


Given below is the syntax of the Python built-in min() function.

min() Parameters

The parameters of the min() function varies and depends upon for what purpose you want to make use of this function. For example, if you want to find the smallest item in a given iterable, then this function will only take the single argument and that is the iterable object itself, even it can take multiple iterables and if you want to find the smallest argument among two or more arguments, then this function can take any number of arguments.

Parameters of min() if finding the smallest item in an iterable

  • iterable. This is an iterable object such a Python List, Tuple, Dictionary, Set, etc. in which you want to find out the smallest item.
  • *iterables. This indicates that you can provide any number of iterables as the individual arguments to this function. It is completely optional to provide multiple iterables.

Related Article. Python *args and **kwargs for Variable Arguments

  • key. Here you can provide a key function. This function will be used to find out the minimum value on the basis of the comparison with its return value. This is an optional argument.
  • default. This is also an optional argument that defines the value that this function should return if the given iterable is empty.

Note. The default argument has been added in Python Version 3.4, so it won’t work on older versions.

Parameters of min() if finding the smallest argument among multiple arguments

  • arg1. This can be any object which you want to compare with other objects that are also passed as arguments to this function.
  • arg2. Again, this can also be any object, similar to the arg1.
  • *args. This means that you can provide any more numbers of arguments that are basically objects of any data type that you want to involve in the comparison done by this function. Again, it is completely optional to provide more than two objects to this function.
  • key. This is the key function that returns a value and the comparison is performed on the basis of that value. Every other argument provided in this function for comparison is also passed to his function and accordingly its return value is calculated.

min() Return Value

This function min() simply returns the smallest item present in the iterable(s) passed as the argument(s) to this function or the smallest argument among the passed objects as the arguments.


A few examples are given below to help you better understand the usage of the Python built-in min() function.

Example 1. Finding the smallest item in a given Iterable

#An Iterable (List)
numbers = [100, 90, 27, 45, 20, 21, 85, 43]

#Finding the smallest number in the numbers List
smallestItem = min(numbers)

print("Smallest Value in the Iterable", numbers, "is", smallestItem)


Example 2. Finding the smallest argument among multiple arguments

#Passing Multiple Number Objects to the min() Function
smallestArgument = min(10, 20.65, 5, 2, 1.5, 100, 25)

print("The Smallest Argument is", smallestArgument)


Example 3. Finding the smallest string in a given list of strings

#list iterable containing string items
names = ["Gurmeet", "Deepinder", "Raman", "Rahul", "Sheila"]

#Finding the smallest string (that comes first in Ascending Order)
smallestName = min(names)

print("The smallest name is", smallestName)


Example 4. Using the function min() for Python Dictionary Iterables

If you pass a dictionary object as the iterable argument to the min() function, then it checks the smallest index of the dictionary. As the smallest index in the students dictionary is 0, hence it returns the key-value 0 as the smallest item.

#A Dictionary Object
students = {
    0: "Simrandeep Kaur",
    1: "Gurmeet Singh",
    2: "Manmeet Singh",
    3: "Parminder Kaur",
    4: "Amandeep Singh"

print("The smallest item in students is", min(students))


Example 5. Using the key function to find the minimum value

You can specify a key function as one of the arguments of the min() function to decide on what basis the comparison will be done. This is illustrated in the following example.

#A Python Tuple
numbers = (5, 2, 12, 13, -15, -14)

def doubleSquare(item):
    return item*item*item*item;

smallestAsPerKey = min(numbers, key = lambda item: doubleSquare(item))

print("Smallest Value as Per Key-Function Logic:", smallestAsPerKey)
print("Smallest Value in General:", min(numbers))

In this example, according to the key function, the smallest item is that whose double square is the smallest. As the double square of 2 i.e. 2*2*2*2 = 16 is the smallest, hence 2 is the smallest value as per the Key-Function logic. But in general, the smallest value is -15.

You’ve to use the lambda function as illustrated in the above code snippet to get the current item of the provided iterable to further pass it to the actual function that returns the value. Here our Lambda function is actually calling doubleSqaure() function and making using of the returned value for comparison of every single item in the iterable that is passed as the first argument of the min() function.


Example 6. The purpose of the default argument in the min() Function

The min() function raises ValueError exception in case the iterable passed as the argument to this function is empty and if you want this exception to be avoided, you have to specify the value for the default argument. The value specified in the default argument is returned when the iterable passed as the argument is empty as illustrated in the following example.

#Providing Empty Iterable and Default Argumument Value
emptyDictionary = {}

smallestValue = min(emptyDictionary, default = "Empty Iterable!")


#Providing Empty Iterable without Default Argumument Value
smallestValue = min(emptyDictionary)


Example 7. Working of min() with Multiple Iterables

The following example illustrates how you can make use find the smallest iterable among multiple iterables using the min() function.

Note. If multiple iterables are passed as the arguments to this function, it returns the smallest iterable not the smallest iterable item among all of the other iterable items collectively.

list1 = [10, 9, 1, 2, 92, 30]
list2 = [0, 0.5, 2.5, 9, 7, 1000]
list3 = [100, 200]

smallestValue = min(list1, list2)

#With Key Function
smallestValue = min(list1, list2, key = lambda item: sum(item))


[0, 0.5, 2.5, 9, 7, 1000]
[10, 9, 1, 2, 92, 30]

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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