Learn Python
>>> functions.higher_order_functions

Higher-Order Functions (map, filter, reduce) in Python

#concept

A higher-order function is one that takes another function as an argument, returns a function, or both. Python's map(), filter(), and reduce() are the classic built-in examples: map() applies a function to every item in a collection, filter() keeps only the items a function approves of, and reduce() combines every item into a single accumulated result.

#how it works
1

map(function, iterable)

Applies function to every item, returning an iterator of the results.

2

filter(function, iterable)

Keeps only the items where function returns something truthy.

3

reduce(function, iterable)

From the functools module, repeatedly combines items two at a time into a single final value.

4

Any function works, including a lambda

As the first argument to any of the three.

5

map() and filter() are lazy

Wrap the result in list() to see or use all the values as a full list.

#example
from functools import reduce
 
numbers = [1, 2, 3, 4, 5]
 
doubled = list(map(lambda n: n * 2, numbers))
print(doubled)
 
evens = list(filter(lambda n: n % 2 == 0, numbers))
print(evens)
 
total = reduce(lambda acc, n: acc + n, numbers)
print(total)
Output
[2, 4, 6, 8, 10]
[2, 4]
15
1map(lambda n: n * 2, numbers) applies the doubling function to every number, producing a lazy iterator that list() then collects into [2, 4, 6, 8, 10].
2filter(lambda n: n % 2 == 0, numbers) keeps only the numbers where n % 2 == 0 is true, giving [2, 4].
3reduce(lambda acc, n: acc + n, numbers) starts combining from the first two numbers, then keeps combining the running total (acc) with each next number, ending with the single value 15.
#use cases
Transforming every item in a collection without writing an explicit loop, similar to a list comprehension
Filtering data down to items matching a condition, as an alternative to a comprehension with an if
Computing a single aggregate value from a collection, like a running total or a combined string
Working with an existing named function (not just a lambda) as an argument, like list(map(str, numbers))

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.