Putting min() and max() Into Action
You’ve learned how
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.
Now you are ready to start coding a few practical examples that will show you how to use
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
max() to select the value that you want to remove from the underlying list.
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,
max() provide the arguments to
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
max() along with a list comprehension.
The second comprehension does a similar task but uses
max() to create a list containing the largest values. Even though
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.
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.
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.
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.
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.
Become a Member to join the conversation.