Python 3.14 ·
✓ verified by execution on 2026-07-28
Lambda, Map, Filter, and Functools
Python provides several functional programming tools that allow you to write concise, expressive code. Let’s explore lambda, map, filter, and the functools module.
Lambda Functions
Small anonymous functions can be created with the lambda keyword. Lambda functions can be used wherever function objects are required. They are syntactically restricted to a single expression.
python
add = lambda x, y: x + yprint(add(5, 3))
Output
8
Your output
You can use lambdas as sorting keys:
python
users = [{'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}]users_sorted = sorted(users, key=lambda u: u['age'])print([u['name'] for u in users_sorted])
Output
['Bob', 'Alice']
Your output
Map and Filter
map() returns an iterator that applies a function to every item of an iterable, yielding the results.
nums = [1, 2, 3, 4]res = map(lambda x: x * 3, filter(lambda x: x % 2 == 0, nums))print(list(res))
Output
[6, 12]
Your output
Functools
The functools module provides higher-order functions. reduce() applies a function of two arguments cumulatively to the items of an iterable, from left to right, reducing the iterable to a single value.
python
from functools import reducenums = [1, 2, 3, 4]product = reduce(lambda x, y: x * y, nums)print(product)
Output
24
Your output
partial() returns a new partial object which when called behaves like func called with the positional arguments and keyword arguments pre-filled.
python
from functools import partialdef power(base, exp): return base ** expsquare = partial(power, exp=2)print(square(5))
Output
25
Your output
Check yourself
Is the following statement true or false? 'lambda functions are faster than regular def functions.'
Reveal answer
False — Lambda functions are just syntactic sugar for a normal function definition. They have exactly the same execution speed.
Is the following statement true or false? 'map and filter return lists in Python 3.'
Reveal answer
False — In Python 3, map() and filter() return iterators, not lists. You must wrap them in list() if you need a physical list, or iterate over them.
Is the following statement true or false? 'A lambda can contain multiple expressions or statements.'
Reveal answer
False — A lambda is strictly limited to a single expression and cannot contain statements like print or return.
Challenges
Challenge 1 +50 XP
Use `filter()` to keep only odd numbers from `data`, and then `map()` to triple them. Finally, convert the result to a list.
python
Test 1 — expects "[3, 9, 15]\n"
Show solution (0 XP)
data = [1, 2, 3, 4, 5]result = list(map(lambda x: x * 3, filter(lambda x: x % 2 != 0, data)))print(result)
Challenge 2 +50 XP
Use `functools.reduce()` and a `lambda` to find the maximum value in `numbers`.
python
Test 1 — expects "99\n"
Show solution (0 XP)
from functools import reducenumbers = [10, 42, 17, 99, 23]maximum = reduce(lambda a, b: a if a > b else b, numbers)print(maximum)
Challenge 3 +50 XP
Bug Hunt! The code below tries to print the `squares` map object twice. But the second print outputs an empty list because the iterator is exhausted. Fix the code so it correctly prints the squared list twice.