Lambda & Higher-Order Functions in Python

From the Python cheat sheet · Functions · verified Jul 2026

Lambda & Higher-Order Functions

Anonymous functions and built-in functions that take functions as arguments.

python
# Lambda — anonymous inline function
square = lambda x: x ** 2
add = lambda a, b: a + b

# map — apply function to each item
list(map(str.upper, ["a", "b"]))  # ["A", "B"]

# filter — keep items where function returns True
list(filter(lambda x: x > 0, [-1, 0, 1, 2]))

# sorted with key
sorted(users, key=lambda u: u["age"])
💡 Prefer list comprehensions over map/filter for readability
⚡ any() and all() short-circuit — great for validation checks
📌 sorted() always returns a new list; .sort() modifies in place
🟢 filter(None, items) removes all falsy values (None, 0, "", [])
lambdamapfilterhigher-order

More Python tasks

Back to the full Python cheat sheet