SyntaxStudy
Sign Up
Python Intermediate 8 min read

Lambda Functions

Lambda Functions

Anonymous single-expression functions defined with lambda.

Syntax

lambda arguments: expression

Examples

square = lambda x: x ** 2
print(square(5))  # 25

add = lambda a, b: a + b
print(add(3, 4))  # 7

With map / filter / sorted

nums = [1, 2, 3, 4, 5]

# map: apply to each element
squares = list(map(lambda x: x**2, nums))

# filter: keep matching
evens = list(filter(lambda x: x % 2 == 0, nums))

# sorted with key
people = [("Bob", 25), ("Alice", 30), ("Eve", 20)]
people.sort(key=lambda p: p[1])
Pro Tip

Lambdas are best for short, throwaway functions — use def for anything complex.