Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Putting min() and max() Into Action

00:00 Putting min() and max() Into Action. So far, you’ve learned the basics of using min() and max() to find the smallest and largest values in an iterable or series of individual values.

00:11 You’ve learned how min() and max() work with different built-in Python data types, such as numbers, strings, and dictionaries, and you’ve also explored how to tweak the standard behavior of these functions and how to use them with list comprehensions and generator expressions.

00:25 Now you are ready to start coding a few practical examples that will show you how to use min() and max() in your own code. To kick things off, you’ll start off with a short example of how to remove the minimum and maximum values from a list of numbers. To do that, you can call .remove() on your input list. Depending on your needs, you’ll use min() or max() to select the value that you want to remove from the underlying list.

01:04 In these examples, the minimum and maximum values in sample could be outlier data points that you want to remove so they don’t affect the further analysis. Here, min() and max() provide the arguments to .remove().

01:19 Now, let’s say you have a list of lists representing a matrix of numeric values, and you need to build lists containing the smallest and largest values from every row in the input matrix. To do this, you can use min() and max() along with a list comprehension.

01:46 The first comprehension iterates over the sublist in matrix and uses min() to build a list containing the smallest value from each sublist.

01:56 The second comprehension does a similar task but uses max() to create a list containing the largest values. Even though min() and max() provide a quick way to deal with the examples in this section, the NumPy library is highly recommended when it comes to processing matrices in Python. This is because NumPy has specific and optimized tools for the job.

02:18 Sometimes you have a list of numeric values and want to clip them to the edges or limits of a given in interval. For example, if a given value is greater than the interval’s upper limit, then you need to convert it down to that limit.

02:31 To do this operation, you can use min(). This may seem like a strange choice, as you’re dealing with large values. The point is that you need to compare each large value to the interval’s upper limit and choose the smaller of the two.

02:45 You’ll set all large values to the prescribed upper limit.

03:03 The call to min() compares every number to the interval’s upper limit. If the target number is greater than the limit, then min() returns that limit.

03:12 The net effect is that any value greater than the limit is now clipped to it. In this example, the numbers 200 and 142 are clipped to 100.

03:24 In contrast, if you want to clip small values to the interval’s lower limit, then you can use max() as seen on-screen.

03:39 The call to max() clips the small values to the interval’s lower limit. max() compares the current number and the interval’s limit to find the maximum value. In this example, -230 is the only number that gets clipped.

03:52 Finally, you can run both operations in one go by combining min() and max(). Here’s how to do it.

04:06 This comprehension combines min() and max(). The call to min() compares the current value to the interval’s upper limit, while the call to max() compares the result to the lower limit.

04:16 The final result is that values lower or greater than the corresponding limit are clipped to the limit itself. This comprehension works similarly to the clip() function in NumPy, which takes an array and the limits of the target interval, and then it clips all values outside the interval to the interval’s edges.

04:36 Let’s say you have a list of tuples containing pairs of values that represent Cartesian points. You want to process all these pairs of points and find out which pair has the smallest distance between them. In this situation, you could use the code seen on-screen.

04:53 First, you import math to get access to dist(). The function returns the Euclidean distance between two points, p and q, each given as a sequence of coordinates.

05:05 The two points must have the same number of dimensions.

05:28 The min() function works its magic through the key argument. In this example, key takes a lambda function that computes the distance between two points.

05:37 This function becomes the comparison criteria for min() to find the pair of points with the minimal distance between them. Here you need a lambda function because key expects a single-argument function, while math.dist() requires two arguments.

05:52 So, the lambda function takes a single argument, points, and then unpacks it into two arguments to feed into math.dist().

06:02 In the next section of the course, you’ll explore more uses of min() and max().

Become a Member to join the conversation.