lambda, map and filter

Small anonymous functions, the two builtins that take them, and why a comprehension usually reads better.

Overview

What a lambda is

square = lambda n: n * n

That is the same as a two-line def, minus the name. The body is a single expression and its value is returned automatically — there is no return, and no room for a statement. lambda n: print(n); return n is a syntax error.

Assigning a lambda to a name, as above, is the one usage style guides actively discourage: if it deserves a name it deserves a def, which also gives it a useful name in tracebacks.

lambda.py

lambda.py Python 3
Output

                    

map_filter_lazy.py

map_filter_lazy.py Python 3
Output

                    

Worth knowing

A lambda holds one expression and returns it. No statements, no return.
map and filter are lazy - wrap in list() to see them.
map(int, words) needs no lambda. map(lambda w: int(w), words) adds nothing.
A comprehension usually reads better; sorted(key=...) is where lambdas shine.

lambda, map and filter: A Practical Guide

A lambda is a function written inline as an expression. map and filter are the two builtins that take one. All three are worth knowing, and in most Python code a comprehension is the better choice.

Where a lambda earns its place

sorted(people, key=lambda p: p[1])

As an argument, where the function is tiny, used once, and naming it would add a line without adding meaning. sorted, min, max and groupby are where you will actually write them.

map and filter

map(f, items)     # apply f to each
filter(f, items)  # keep those where f is true

Both are lazy: they return iterators, not lists. Printing one shows <map object>, and consuming it twice gives nothing the second time — the page demonstrates exactly that, because it catches people.

The comprehension equivalents are usually clearer:

[n * n for n in nums]       # instead of map
[n for n in nums if n % 2 == 0] # instead of filter

They read left to right, they do not need list() around them, and they do not need a lambda at all.

When map is genuinely better

Two cases. First, when the function already exists:

map(int, words)

No lambda, no comprehension variable — just "convert each of these". Writing map(lambda w: int(w), words) instead is pure ceremony.

Second, when consuming several sequences in parallel:

map(lambda x, y: x + y, a, b)

though [x + y for x, y in zip(a, b)] says the same thing and most readers will parse it faster.

The judgement

Know all three, because you will read them. Write comprehensions by default, map(existing_function, items) when it is that shape exactly, and lambdas mainly as key= arguments.

Check yourself

0 of 3

Answer without scrolling back up.

  1. What can a lambda body contain?

  2. `list(m)` twice on a map object gives what the second time?

  3. Which is preferred: `map(lambda w: int(w), ws)` or `map(int, ws)`?

Cheat sheet

lambda, map and filter

A lambda is a function written inline as an expression. map and filter are the two builtins that take one. All three are worth knowing, and in most Python code a comprehension is the better choice.

PYTHON · vizlearn.in/python/lambda_map_filter.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.