Definition
map() applies a function to every item. filter() keeps the items for which a function returns True. functools.reduce() combines items into one value, and the functools module adds tools such as partial and lru_cache.
In simple words
Picture a factory line. map transforms each item, filter throws out bad ones, and reduce squeezes everything into a single result.
Why it matters
You'll read these in other people's code. lru_cache is particularly useful, as one line can make a slow function vastly faster.
How it works
In code
from functools import lru_cache, partial, reduce
numbers = [1, 2, 3, 4, 5]
print(list(map(lambda x: x * x, numbers)))
print(list(filter(lambda x: x % 2 == 0, numbers)))
print(reduce(lambda total, x: total + x, numbers))
def power(base, exponent):
return base ** exponent
square = partial(power, exponent=2)
print(square(5))
@lru_cache(maxsize=None)
def fibonacci(n):
return n if n < 2 else fibonacci(n - 1) + fibonacci(n - 2)
print(fibonacci(80))
print(fibonacci.cache_info().hits, "answers reused from the cache")Output
[1, 4, 9, 16, 25] [2, 4] 15 25 23416728348467685 78 answers reused from the cache
Common mistakes
- Forgetting that map and filter are lazy. Wrap them in list() to see the results.
- Using lru_cache on functions with unhashable arguments, like lists, which raises an error.
Check yourself
Summary
- map transforms, filter selects, and reduce combines.
- Comprehensions are usually clearer.
- lru_cache gives easy speed-ups.
Finished reading?
Sources: The Python Tutorial, python.org