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
map_filter_lazy.py
Worth knowing
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.sorted(key=...) is where lambdas shine.