5.15 Other Sequence Processing Functions

Python provides other built-in functions for manipulating sequences.

Finding the Minimum and Maximum Values Using a Key Function

We’ve previously shown the built-in reduction functions min and max using arguments, such as ints or lists of ints. Sometimes you’ll need to find the minimum and maximum of more complex objects, such as strings. Consider the following comparison:

In [1]: 'Red' < 'orange'
Out[1]: True

The letter 'R' “comes after” 'o' in the alphabet, so you might expect 'Red' to be less than 'orange' and the condition above to be False. However, strings are compared by their characters’ underlying numerical values, and lowercase letters have higher numerical values than uppercase letters. ...

Get Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.