search

How to find the minimum value in a list in Python?

To find the minimum value in a list in Python, you can use the built-in min() function. Here's an example:

my_list = [4, 2, 9, 1, 7]
minimum = min(my_list)
print(minimum)
# Output: 1

In this example, the min() function is used to find the minimum value in the list my_list. The resulting minimum value is then stored in the variable minimum and printed.

The min() function takes an iterable, such as a list, as an argument and returns the minimum value in that iterable.

Note that the min() function returns the minimum value based on the default comparison, which is the natural ordering of the elements. If the list contains elements of different types, they should be compatible for comparison (e.g., numbers or strings). If you have a list of complex objects and want to find the minimum based on a specific attribute or custom comparison, you can use the key parameter of the min() function or use a lambda function in conjunction with min() to define a custom comparison criterion.

Related Questions You Might Be Interested