Latticework

Command Palette

Search for a command to run...

Functional Programming Fundamentals

Map, Filter, Reduce

18 min

Explanation

map, filter, and reduce are the three core building blocks of functional-style data processing — transform every element, keep only some elements, and collapse everything into one value. You've already been writing their logic as comprehensions; this module names the underlying functions explicitly.

nums = [1, 2, 3, 4, 5]

squared = list(map(lambda x: x**2, nums))          # [1, 4, 9, 16, 25]
evens = list(filter(lambda x: x % 2 == 0, nums))    # [2, 4]

map(f, iterable) applies f to every element. filter(pred, iterable) keeps only elements where pred returns truthy. Both return a lazy iterator, not a list — wrap in list(...) to see the actual values, or just iterate directly.

Try it

map/filter and comprehensions express the exact same computation -- most Python style guides prefer comprehensions for readability, but map/filter/reduce are the vocabulary shared across functional languages generally, worth recognizing.

Loading editor…
Explanation

reduce has no comprehension equivalent — it collapses an entire sequence into a single value by repeatedly combining an accumulator with the next element:

from functools import reduce

nums = [1, 2, 3, 4]
total = reduce(lambda acc, x: acc + x, nums, 0)   # ((((0+1)+2)+3)+4) = 10
product = reduce(lambda acc, x: acc * x, nums, 1)  # ((((1*1)*2)*3)*4) = 24

The third argument (0, 1 above) is the starting value for the accumulator — get this wrong (e.g. starting a product accumulator at 0 instead of 1) and every result collapses to 0, a common bug when first learning reduce. sum() and max() are really just reduce with the combining function built in.

Exercise

Write `squares_over_10(nums)` using `map` and `filter` (not a comprehension): square every number, then keep only the squares greater than 10. Return the result as a list.

Exercise

Write `product_of_evens(nums)` using `functools.reduce`: multiply together every even number in `nums` (return 1 if there are none).

Quiz

What does functools.reduce(f, items, initial) do?

Checkpoint

You can use map and filter explicitly (not just as comprehensions), and use reduce to collapse a sequence into a single accumulated value.