Using Python’s map(), reduce(), and filter() Functions Together

Python provides three useful functions that operate on iterable collection objects:

  • map() applies a function to each element in an iterable.
  • filter() selects elements from an iterable based on a condition.
  • reduce() aggregates the items of an iterable to a single value.

These can be combined to form a widely-applicable tool for conveniently transforming and processing data.

Let’s consider an example that uses filter(), map(), reduce() to process classroom data for a school. The data consists of a list of dictionaries representing classrooms records. Each record has an associated grade level and a list of children’s names.

In the example, we’ll use filter() to get an iterable of all 1st-grade classes, then use map() to extract the list of names from each record, and finally, use reduce() to combine all lists together.

The end result will be a list of the names of all 1st-grade children.

Example:

from functools import reduce

# Sample data: List of dictionaries representing classrooms
classrooms = [
    {'grade': 1, 'children': ['Alice', 'Bob', 'Charlie']},
    {'grade': 2, 'children': ['David', 'Eve', 'Frank']},
    {'grade': 1, 'children': ['Grace', 'Harry', 'Isabel']},
    {'grade': 3, 'children': ['Jack', 'Lily', 'Mike']},
]

first_grade_classrooms = filter(lambda classroom: classroom['grade'] == 1, classrooms)
first_grade_children_lists = map(lambda classroom: classroom['children'], first_grade_classrooms)
first_grade_children = reduce(lambda acc, children_list: acc + children_list, first_grade_children_lists, [])
printable_list = ', '.join(first_grade_children)

print(f"The list of all 1st-grade children is: {printable_list}")

Output:

The list of all 1st-grade children is: Alice, Bob, Charlie, Grace, Harry, Isabel

In this example:

  • filter(lambda classroom: classroom['grade'] == 1, classrooms): Filters out only the 1st-grade classrooms.
  • map(lambda classroom: classroom['children'], first_grade_classrooms): Extracts the list of children’s names for each 1st-grade classroom.
  • reduce(lambda acc, children_list: acc + children_list, first_grade_children_lists, []): Combines the lists into a single list.
  • ', '.join(first_grade_children): Joins the list of children’s name into a single comma-separated string of names that is ready for printing.

Summary

Combining map(), filter(), and reduce() provides a powerful and concise way to manipulate data in Python. These functions, when used together, enable you to express complex operations on iterables in an elegant and readable way.