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

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Creating the min_max() Function

00:00 It’s time to start creating your custom min_max() function. Fire up your favorite code editor or IDE and create a new Python file called min_max.py. Then add the code seen on-screen.

00:19 Here you start to define min_max(). The function’s first portion standardizes the input data for further processing. Because the user will be able to call min_max() with either a single iterable or with several regular arguments, you need to check the length of args.

00:37 To do this, you use the built-in len() function. If args holds only one value, then you need to check that the argument is an iterable object. You use list(), which implicitly does the check and also turns the input iterable into a list.

00:52 If list() raises a TypeError, then you catch it and raise your own TypeError to inform the user that the provided object isn’t iterable, just like min() and max() do in their standard form.

01:08 Note that you use the from None syntax to hide away the traceback of the original TypeError. The else branch runs when args holds more than one value, which handles the cases where the user calls the function with several regular arguments instead of with a single iterable of values.

01:26 If this conditional doesn’t ultimately raise a TypeError, then values will hold a list of values that may be empty. Even if the resulting list is empty, it’s now clean and ready for continuing the process of finding its minimum or maximum value.

01:42 Here, you define a conditional to check if values holds an empty list. If that’s the case, then you check the default argument to see if the user provided a value for it.

01:52 If default is still None, then a ValueError is raised.

01:59 Otherwise default is returned. This behavior emulates the standard behavior of min() and max() when you call them with empty iterables. Next, you need to process the key argument and prepare the data for finding the smallest and largest values according to the provided key. First, you check if the user hasn’t provided a key function.

02:22 If they haven’t, then you create a list of keys directly from your original values. You’ll use these keys as comparison keys in computing the minimum and maximum.

02:33 On the other hand, if the user has provided a key argument, then you need to make sure that the argument is actually a function or callable object. To do this, you use the built-in callable() function, which returns True if the argument is callable and False otherwise.

02:49 Once you’re sure that key is callable, then you build a list of comparison keys by applying key to each value in the input data. Finally, if key isn’t a callable object, then the else clause runs, raising a TypeError, just as min() and max() do.

03:10 The last step to finish your min_max() function is to find the minimum and maximum values in the input data, just as min() and max() do.

03:18 You’ll set the extreme_key and extreme_value variables to the first value in keys and values, respectively.

03:25 These variables will provide the initial key and value for computing the minimum and maximum. Then you loop over the remaining keys and values in one go using the built-in zip() function.

03:37 This function will yield key-value tuples by combining the values in your keys and values lists.

03:48 The conditional inside the loop calls operator to compare the current key to the tentative minimum or maximum key stored in extreme_key.

03:57 At this point, the operator argument will hold either lt() or gt() from the operator module, depending on if you wish to find the minimum or maximum value, respectively.

04:08 Every loop operation compares the current key to the tentative minimum or maximum and updates the values of extreme_key and extreme_value. Accordingly, at the end of the loop, these variables will hold the minimum or maximum key and its corresponding value. Finally, you just need to return the value in extreme_value.

04:33 In the next section of the course, you’ll complete the implementation of your custom min() and max() functions.

cmadsoneng on Oct. 11, 2023

Why are we doing “if callable(key)”, and not “if callable(operator)”?

cmadsoneng on Oct. 11, 2023

Never mind… just got it.

Become a Member to join the conversation.