min() in Python
Overview:
The min() is a built-in method used to find the minimum value in the list or, in the case of the list of Strings, the String with the smallest lexicographic value is returned.
Syntax of min() function in Python
We can write the syntax of the min() method in the following two ways:
When iterable is not passed in min() function
Syntax:
When iterable is passed in min() function
Syntax:
Parameters of min() function in Python
The different parameters of the min() function in Python are as follows:
- iterable (required): An iterable object like string, list, tuple etc. We can also pass elements directly to the min() function instead of using an iterable.
- default (optional): The default value to return if the iterable is empty.
- key (optional): It refers to the single argument function to customize the sort order. The function is applied to each item on the iterable.
Return values of min() function in Python
The min() method returns the minimum element in the case of integers or floating type values. In the case of Strings, the min() method returns the smallest lexicographically order string. If we apply the min() method on a String, it will return the lexicographically smallest character in the String.
Exceptions of min() function in Python
The min() method raises the TypeError Exception when we pass a list containing elements of the different data types.
Example of min() function in Python
Let's understand the usage of the min() method using an example.
We will find the minimum number from the set of numbers and the lexigraphically smallest character from another list.
Code:
Output:
Explanation:
1 is the minimum element among all elements, and 'a' character is the smallest character among all the characters with respect to the ASCII value.
What is min() function in Python ?
Let's suppose you have two types of lists that are given below:
- A List that contains only numbers.
- A List that contains Strings of the same length.
Now your task is to find the smallest number present in the first list and the smallest lexicographically order String in the second list.
How do you find the required answers? We can get all our required answers using the min() method. Let's understand min() in python better to get the answer to our questions.
The min() method is an inbuilt method in Python that takes a list of the same datatype elements and returns the smallest value in the case of number, and in the case of the String, the min() method returns the smallest lexicographically order String.
The min() method can also take a number of comma-separated items of the same data type or an iterable of the same data type.
More Examples
Example 1: To get the smallest item from a List
Let's understand how to find the smallest item in a list using the min() method.
We have passed a list containing integer elements in the min() method in the below program.
Code:
Output:
Example 2: To get the smallest String in a List
Let's understand how to find the smallest string in the list using the min() method. In the below program, we have a list that contains strings of different lengths, and we have passed the list as a parameter in the min() method, and also we have passed the second parameter key, which is initialized by the length. It will return the smallest string in the list by comparing the length of all the Strings.
Code:
Output:
Example 3: Using min() with Dictionaries in Python
Let's understand how to find the minimum key in the given dictionary.
Code:
Output:
The -12 is the minimum key present in the given dictionary.
Example 4: Case of TypeError Exception
In the below example, we have passed a list that contains both integer elements as well as String elements this will cause TypeError because the min() method only compares the elements of the same type.
Code:
Output:
Conclusion
- min() method is an inbuilt method in Python.
- min() method cannot take zero parameters.
- min() method returns the minimum number in the case of numbers, and in the case of String parameters, it will return the smallest lexicographically order String.