Lambda Functions in Python
A lambda is a small, anonymous function written in a single expression, without a name or a def statement. It's most useful for a short piece of logic needed briefly, often passed directly as an argument to another function, rather than being defined and named separately.
lambda parameters: expression
Defines the function in one line — no def, no name, no return keyword.
The expression is returned automatically
There's no explicit return statement needed or allowed.
Can be assigned to a variable
Though a regular def is usually clearer for anything that needs a lasting name.
Most often passed directly
As the key for sorting, or the function argument to map()/filter().
Limited to a single expression
A lambda can't contain multiple statements, loops, or assignments.
square = lambda x: x ** 2print(square(5)) people = [("Ada", 36), ("Grace", 85), ("Alan", 41)]people.sort(key=lambda person: person[1])print(people)Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.