Lesson 20 / 47

Lambda Functions

Small, anonymous, one-expression functions.

One-line functions

A lambda is a nameless function limited to a single expression — handy as a short-lived argument.

square = lambda n: n * n
print(square(5))   # 25

people = [("Ada", 36), ("Bo", 24)]
people.sort(key=lambda p: p[1])
print(people)

When to use lambda

Use lambda for throwaway callbacks (sort, filter, map); for anything with logic or a name worth reusing, write a normal def.