Combining map and reduce transformations
In the other recipes in this chapter, we've been looking at map, filter, and reduce operations. We've looked at each of these in isolation:
The Applying transformations to a collection recipe shows the
map()
functionThe Picking a subset – three ways to filter recipe shows the
filter()
functionThe Summarizing a collection – how to reduce recipe shows the
reduce()
function
Many algorithms will involve combinations of functions. We'll often use mapping, filtering, and a reduction to produce a summary of available data. Additionally, we'll need to look at a profound limitation of working with iterators and generator functions. Namely this limitation:
Tip
An iterator can only produce values once.
If we create an iterator from a generator function and a collection data, the iterator will only produce the data one time. After that, it will appear to be an empty sequence.
Here's an example:
>>> typical_iterator = iter([0, 1, 2, 3, 4])
>>>...