Intermediate·6 min·intermediate · functional
Lambda, map, filter
Lambda, map, filter
lambda args: expr — an unnamed function for short one-liners. Don't write multi-line lambdas; define a real function instead.
The Pythonic preference
[f(x) for x in seq] is usually clearer than list(map(f, seq)). But map/filter shine when passing functions around or chaining.
Where you'll really use lambdas
sorted(items, key=lambda x: x.name) — providing a key function inline.
Try it
- Sort
[("Ada", 30), ("Bob", 25)]by age. - Use
filterto keep only positive numbers from[-2, -1, 0, 1, 2].