• Print
This chapter is from the book

## 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 : 'Red' < 'orange'
Out: 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. You can confirm this with built-in function ord, which returns the numerical value of a character:

```In : ord('R')
Out: 82

In : ord('o')
Out: 111```

Consider the list colors, which contains strings with uppercase and lowercase letters:

`In : colors = ['Red', 'orange', 'Yellow', 'green', 'Blue']`

Let’s assume that we’d like to determine the minimum and maximum strings using alphabetical order, not numerical (lexicographical) order. If we arrange colors alphabetically

`'Blue', 'green', 'orange', 'Red', 'Yellow'`

you can see that 'Blue' is the minimum (that is, closest to the beginning of the alphabet), and 'Yellow' is the maximum (that is, closest to the end of the alphabet).

Since Python compares strings using numerical values, you must first convert each string to all lowercase or all uppercase letters. Then their numerical values will also represent alphabetical ordering. The following snippets enable min and max to determine the minimum and maximum strings alphabetically:

```In : min(colors, key=lambda   s: s.lower())
Out: 'Blue'

In : max(colors, key=lambda   s: s.lower())
Out: 'Yellow'```

The key keyword argument must be a one-parameter function that returns a value. In this case, it’s a lambda that calls string method lower to get a string’s lowercase version. Functions min and max call the key argument’s function for each element and use the results to compare the elements.

#### Iterating Backward Through a Sequence

Built-in function reversed returns an iterator that enables you to iterate over a sequence’s values backward. The following list comprehension creates a new list containing the squares of numbers’ values in reverse order:

```In : numbers = [10, 3, 7, 1, 9, 4, 2, 8, 5, 6]

In : reversed_numbers = [item for   item in reversed(numbers)]

In : reversed_numbers
Out: [36, 25, 64, 4, 16, 81, 1, 49, 9, 100]```

#### Combining Iterables into Tuples of Corresponding Elements

Built-in function zip enables you to iterate over multiple iterables of data at the same time. The function receives as arguments any number of iterables and returns an iterator that produces tuples containing the elements at the same index in each. For example, snippet ’s call to zip produces the tuples ('Bob', 3.5), ('Sue', 4.0) and ('Amanda', 3.75) consisting of the elements at index 0, 1 and 2 of each list, respectively:

```In : names = ['Bob', 'Sue', 'Amanda']

In : grade_point_averages = [3.5,   4.0, 3.75]

In : for name, gpa in zip(names, grade_point_averages):
...:     print(f'Name={name}; GPA={gpa}')
...:
Name=Bob; GPA=3.5
Name=Sue; GPA=4.0
Name=Amanda; GPA=3.75```

We unpack each tuple into name and gpa and display them. Function zip’s shortest argument determines the number of tuples produced. Here both have the same length.