Best April deals - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Built-in Functions Common to Most Data Structures
2. min() and max()
Summary

Instruction

Okay, let's get started.

Finding the smallest or largest element in a data structure is such a common task that Python gives us two built-in utility functions: min() and max(). Both functions work with any data structure that can be iterated over with a for loop: lists, sets, tuples, and even strings.

Suppose we have a list that represents the number of customer requests processed per day:

requests_processed = [12, 18, 15, 23, 24, 8, 0, 18, 14, 13]

To find the maximum number of requests processed, we use:

max(requests_processed)

And to find the minimum number:

min(requests_processed)

As you can see, min() and max() are extremely easy to use.

Exercise

When used with a list of strings, min() and max() will return the first or last string (according to alphabetical order).

Write a function named find_first_last(list_strings) that accepts a list of strings and returns a tuple with two elements:

(first_string_alphabetically, last_string_alphabetically)

You can use the sample list of book titles to test your code.

Stuck? Here's a hint!

Return the following tuple:

(min(list_strings), max(list_strings))