Using the groupby() and reduce() functions

A common requirement is to summarize data after partitioning it into groups. We can use a defaultdict(list) method to partition data. We can then analyze each partition separately. In Chapter 4, Working with Collections, we looked at some ways to group and partition. In Chapter 8, The Itertools Module, we looked at others.

The following is some sample data that we need to analyze:

>>> data = [('4', 6.1), ('1', 4.0), ('2', 8.3), ('2', 6.5),... ('1', 4.6), ('2', 6.8), ('3', 9.3), ('2', 7.8),... ('2', 9.2), ('4', 5.6), ('3', 10.5), ('1', 5.8),... ('4', 3.8), ('3', 8.1), ('3', 8.0), ('1', 6.9),... ('3', 6.9), ('4', 6.2), ('1', 5.4), ('4', 5.8)]

We've got a sequence of raw data values with a key and a ...

Get Functional Python Programming - Second Edition 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.