📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Python from Zero Lambda Functions

Lambda Functions

5 min read
Lambda creates short anonymous functions: lambda x: x * 2. Use them as callbacks in sorted(), map(), and filter(). For anything longer than one expression use a named function for better readability.

Lambda (Anonymous) Functions

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

# Sort by key
users = [{'name':'Bob','age':32},{'name':'Alice','age':25}]
users.sort(key=lambda u: u['age'])

# With map and filter
nums = [1, 2, 3, 4, 5]
doubled  = list(map(lambda x: x*2, nums))
evens    = list(filter(lambda x: x%2==0, nums))