Lesson 01
Lambda and Special Functions — Why filter, map, and reduce need callbacks
Meet filter, map, and reduce as special functions, see why they need a FunctionName, and how lambda is the usual instant callback in this lab.
What you will learn
- What the teaching notes call special functions:
filter(),map(),reduce() - Why each one takes a FunctionName plus an iterable
- How lambda fills that slot as an instant operation
- Where
zip()fits when two sequences must travel together - The filter → map → reduce pipeline this lab finishes with
The Functions module already introduced lambda syntax and a first look at filter / map / reduce. This module is the classroom lab: named vs inline callbacks, zip, two-iterable map(), functools.reduce internals, palindrome filters, and the salary-band pipeline from the SPECIAL FUNCTIONS folder.
What are special functions?
Special functions accept another function as their first parameter. In this module the three core functions are filter(), map(), and reduce().
| Function | Purpose |
|---|---|
filter() |
Returns only items that satisfy a condition |
map() |
Applies an operation to every item |
reduce() |
Combines all values into one result |
Different purpose — same pattern: Function + Iterable → Result.
filter and map are built-ins. They return lazy iterators (<class 'filter'>, <class 'map'>). Cast with list(), tuple(), or set() before you print contents.
reduce lives in functools (Python 3):
import functools
# or
import functools as fcWhy they need a FunctionName
The first argument is called FunctionName. It tells the special function what operation to perform. The teaching files show three equivalent ways to provide it.
Lambda syntax:
lambda parameters: expressionThe lambda contains one expression and automatically returns its result. Full syntax and sorted(key=...) live in Lambda functions; this lab uses lambda as the usual instant callback.
- A normal
def—filter(positive, lst) - A named lambda —
positive = lambda val: val > 0thenfilter(positive, lst) - An inline lambda —
filter(lambda val: val > 0, lst)
Style 3 is the usual lab pattern: the operation is used once.
How filter() works
filter() checks every value. If the callback returns True, the value is kept. If it returns False, the value is discarded.
- Take one item from the iterable
- Evaluate the lambda condition
- Keep only the values returning True
Zero is neither > 0 nor < 0, so it is dropped from both a positive filter and a negative filter. Deep dive: filter().
How map() works
map() never removes items. It transforms every value, so the output length stays the same.
map(lambda s: s + s * 0.5, salaries)Every salary receives the same 50% increase (100 → 150, 200 → 300, 300 → 450). Deep dive: map() and zip().
How reduce() works
Unlike filter() and map(), reduce() combines values into one final result. Import it from functools.
- Start with the first value
- Combine with the next value (
m + p) - Store the result in the accumulator (
m) - Repeat until only one value remains
Deep dive: reduce().
Where zip() fits
zip() is used after mapping when multiple sequences should travel together. It pairs values row by row and stops at the shortest iterable.
for no, sq, rt in zip(nums, squares, roots):
print(no, sq, rt)That is why the two-list sum lab pads the shorter list with 0.0 before mapping — zip / map do not pad for you.
Complete data pipeline
This module finishes with a practical pipeline: Filter → Map → Reduce. Each function performs one responsibility before passing data to the next stage.
filter()→ Select datamap()→ Transform datareduce()→ Produce one final value
That is the most common functional workflow in this lab. Combo examples: Filter → Map → Reduce pipeline.
Module map
| Lesson | Focus |
|---|---|
| Lambda as instant operations | Named vs inline, ternary, lambda calling def |
| filter() | True/False keep, palindromes, length 3–4 |
| map() and zip() | Hike, squares, two iterables, padding |
| reduce() | functools, max/min, word concat |
| Pipeline | +ve/−ve sums and salary bands |
Return is not a special function here — it already has its own Functions lesson: return statement.
Common mistakes
- Printing a
filter/mapobject and expecting the values — cast first - Forgetting
import functoolsbeforereduce - Treating
zipas if it padded the shorter sequence — it truncates - Copying a long
definto a lambda — lambda is for one expression - Mapping first, then filtering — hike values you meant to drop
Interview cheat sheet
| Prompt | Answer |
|---|---|
| Special functions in this lab | filter, map, reduce |
| Usual FunctionName | lambda (instant operation) |
reduce import |
functools |
| Pair rows after map | zip |
| Cast iterators | list() / tuple() |
| Pipeline order | filter → map → reduce |
Practice in the playground
The starter already filters positives, maps a 50% hike (sal + sal * (50 / 100)), reduces a sum, and zips. Try:
- Change the filter to negatives (
v < 0). - Swap
list(...)fortuple(...)onfilter. - Add a second
mapthat squares each hiked salary.
Next: Lambda as instant operations.
Loading playground…
Check your understanding
Lambda and special functions overview quiz
Which three functions does this lab treat as special functions?
1 / 10