All lessons
λ

Lambda Functions

Small anonymous functions

Reward
+40 XP

Lambda syntax

A lambda is a small anonymous function: lambda args: expression.

double = lambda x: x * 2
print(double(5))  # 10

Using with map and filter

Lambdas are often used with map(), filter(), and sorted().

nums = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, nums))
print(squared)

Try it yourself

Run the example below. Edit it and see what changes.

main.py
Output
Press Run to execute (Ctrl/Cmd+Enter)

Common mistakes

  • Lambda can only contain a single expression, not statements.
  • Overusing lambdas when a regular def would be clearer.