To get the minimum of a list in Python, we can use the Python min() function. You can use the min() function in Python to get the minimum value of a list of numbers, strings and objects.

list_of_numbers = [10,32,98,38,47,34]

print(min(list_of_numbers))

#Output
10

When working with different collections of data in Python, the ability to be able to summarize and find properties of these pieces of data are valuable.

One such case is if you want to find the minimum value of a list in Python.

You can use the Python min() function to find the minimum, or smallest, value in a list made up of numbers or strings.

Let’s say we have the following list of numbers in Python. You can use the Python built-in min() function to get the minimum value of the list in the following way:

list_of_numbers = [10,32,98,38,47,34]

print(min(list_of_numbers))

#Output
10

For a collection of strings, the smallest value is determined after sorting alphabetically.

print(min("Mike","Alex","Patricia"))

#Output
Abby

Using Lambda Expression with min() Function on List of Objects in Python

Depending on the objects in your list, it might make sense that you need to apply a function or want to find the minimum value based on a certain attribute of the objects in a list.

One cool thing is that you can pass a lambda expression to the “key” parameter to instruct min() what to use to find the minimum value.

One use of a lambda expression with min() is if you want to find the minimum value in list of objects.

For example, if we have a list of numbers and want to take the minimum after applying the absolute value to each element, we can use a lambda.

Below shows a simple example of using a lambda expression with min() in Python.

example_list = [0, 3, 1, -3, -5, 4]

print(min(example_list, key=lambda x:abs(x)))

#Output:
0

Another example is let’s say we have a list of dictionaries and want to find the minimum value over our list for the key “height”.

You can easily do this with “key” and a lambda expression.

Below is an example of how to use a lambda expression with min() to get the minimum value of a list of dictionaries for a given key.

data = [{"name":"Andrew", "age":10, "height":50}, {"name":"Sally", "age":9, "height":45}, {"name":"Jose", "age":11, "height":52}]

print(min(data,key=lambda x: x["height"]))

#Output:
{"name":"Sally", "age":9, "height":45}

If you have a list of tuples, you can use “key” and a lambda as well to get the minimum value of a given position in the tuples.

Below is an example of how to use a lambda expression with min() to get the minimum value of a list of tuples for the first element.

data = [(0,'a'),(3,'d'),(2,'r'),(1,'m')]

print(min(data, key=lambda x: x[0]))

#Output:
(0,'a')

Finding Maximum Value of List with max() in Python

If you want to go the other way and find the max value of a list, you can use the Python max() function.

max() works the same as min and returns the biggest value of the list.

Below is a simple example showing you how to use max() in Python.

list_of_numbers = [10,32,98,38,47,34]

print(max(list_of_numbers))

#Output
98

Hopefully this article has been useful for you to learn how to get the minimum value of a list in Python with min().

Categorized in:

Python,

Last Update: March 11, 2024