Chapter 5. Accumulation operations with reduce

 

This chapter covers

  • Recognizing the reduce pattern for N-to-X data transformations
  • Writing helper functions for reductions
  • Writing lambda functions for simple reductions
  • Using reduce to summarize data

In chapter 2, we learned about the first part of the map and reduce style of programming: map. In this chapter, we introduce the second part: reduce. As we noted in chapter 2, map performs N-to-N transformations. That is, if we have a situation where we want to take a sequence and get a same-sized sequence back, map is our go-to function. Among the examples of this that we’ve reviewed are file processing (we have a list of files and we want to do something to all of them; discussed in chapter 4) and web scraping (we have a list of websites and we want to get the content for each of them; discussed in chapter 2).

5.1. N-to-X with reduce

5.2. The three parts of reduce

Removing the reduce function from base Python

5.2.1. Accumulation functions in reduce

Reducing from left to right

5.2.2. Concise accumulations using lambda functions

5.2.3. Initializers for complex start behavior in reduce

5.3. Reductions you’re familiar with

5.3.1. Creating a filter with reduce

5.3.2. Creating frequencies with reduce

5.4. Using map and reduce together

5.5. Analyzing car trends with reduce

5.5.1. Using map to clean our car data

5.5.2. Using reduce for sums and counts

5.5.3. Applying the map and reduce pattern to cars data

5.6. Speeding up map and reduce

5.7. Exercises

5.7.1. Situations to use reduce

5.7.2. Lambda functions

5.7.3. Largest numbers

5.7.4. Group words by length

Summary

sitemap