Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Intermediate25 min
Lambda plus filter map reduce zip 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().

Same iterable, three special-function results
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):

python
import functools
# or
import functools as fc

Why 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.

def, named lambda, and inline lambda all fill FunctionName

Lambda syntax:

python
lambda parameters: expression

The 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.

  1. A normal def — filter(positive, lst)
  2. A named lambda — positive = lambda val: val > 0 then filter(positive, lst)
  3. 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.

filter keeps True values and drops False
  1. Take one item from the iterable
  2. Evaluate the lambda condition
  3. 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 applies a 50 percent hike to every salary
python
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.

reduce accumulator m plus p becomes one value
  1. Start with the first value
  2. Combine with the next value (m + p)
  3. Store the result in the accumulator (m)
  4. 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.

zip pairs original square and root row by row
python
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 then map then reduce to a final result
  1. filter() → Select data
  2. map() → Transform data
  3. reduce() → 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 / map object and expecting the values — cast first
  • Forgetting import functools before reduce
  • Treating zip as if it padded the shorter sequence — it truncates
  • Copying a long def into 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:

  1. Change the filter to negatives (v < 0).
  2. Swap list(...) for tuple(...) on filter.
  3. Add a second map that 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