Python 3 – List min() Method
In Python programming language, the min()
method is a list method used to find the minimum value of a list containing numbers. This method is commonly used in programming when working with lists.
Syntax
min(list)
Parameters
The min()
method only takes one parameter which is the list containing numbers that you want to find the minimum value from.
Return Value
The min()
method returns the minimum value stored in the list.
Examples
Example 1
The following Python code demonstrates how to use the min()
method to find the minimum value of a list containing numbers.
number_list = [10, 20, 30, 40, 50]
minimum = min(number_list)
print("The minimum value in the list is:", minimum)
Output:
The minimum value in the list is: 10
Example 2
The following Python code demonstrates how to use the min()
method to find the minimum value of a list containing numbers stored as strings.
string_list = ["10", "20", "30", "40", "50"]
minimum = min(string_list)
print("The minimum value in the list is:", minimum)
Output:
The minimum value in the list is: 10
In the above example, even though the elements in the list were stored as strings, the min()
method was still able to find the minimum value, which was ’10’.
Example 3
The following Python code demonstrates how to use the min()
method to find the minimum value of a list containing negative numbers.
negative_list = [-10, -20, -30, -40, -50]
minimum = min(negative_list)
print("The minimum value in the list is:", minimum)
Output:
The minimum value in the list is: -50
Example 4
The following Python code demonstrates how to use the min()
method with the key
parameter. The key
parameter is used to specify a function that takes a single argument and returns a value to be used as the sorting key.
list_of_strings = ["cat", "lion", "dog", "elephant"]
minimum = min(list_of_strings, key=len)
print("The shortest string in the list is:", minimum)
Output:
The shortest string in the list is: cat
In the above example, the key
parameter is used to specify that we want to find the shortest string in the list. The key=len
specifies that we want to use the length of the strings as the sorting key.
Example 5
The following Python code demonstrates how to use the min()
method with an empty list.
empty_list = []
try:
minimum = min(empty_list)
except ValueError:
print("The list is empty.")
Output:
The list is empty.
When an empty list is passed as the argument to the min()
method, it raises a ValueError
exception. To handle this exception, we can use a try
and except
block as shown in the example.
Conclusion
In conclusion, the min()
method in Python is a useful list method that allows us to find the minimum value of a list containing numbers. It is easy to use and can also be used on lists containing elements stored as strings. We can also use the key
parameter to specify a function that takes a single argument and returns a value to be used as the sorting key. In cases where an empty list is passed as the argument, a ValueError
exception is raised and we can handle it using a try
and except
block.