Learn Python lambda functions—anonymous one-expression functions. Master lambda with map, filter, sorted, reduce and understand when to use them.
📌 Python lambda, anonymous function, lambda map filter, lambda sorted, Python functional programming
A lambda function is a small anonymous function that can take any number of arguments but contains only one expression. Lambda functions are perfect for short, throwaway functions where defining a full function would be overkill.
Lambda functions have three key characteristics: they're anonymous (no name like regular functions), limited to one expression (one line of code), and still function as proper functions that take arguments and return results.
The lambda syntax is elegant: lambda arguments: expression. This creates a function object that can be assigned to a variable or passed directly to other functions. It's the equivalent of def but compressed into a single line.
Lambdas shine when used with higher-order functions: map() applies a function to every element, filter() selects elements matching a condition, sorted() with a key parameter for custom sorting, and reduce() combines elements into a single value.
Knowing when NOT to use lambdas is as important as knowing when to use them. If the function is complex, will be reused, or needs documentation—use a regular def function instead. Lambdas should be simple and readable at a glance.
# Regular function
def square(x):
return x * x
# Equivalent lambda
square_lambda = lambda x: x * x
print(square(5)) # 25
print(square_lambda(5)) # 25numbers = [1, 2, 3, 4, 5] # Double each number doubled = list(map(lambda x: x * 2, numbers)) print(doubled) # [2, 4, 6, 8, 10] # Get string lengths words = ["apple", "banana", "cherry"] lengths = list(map(lambda x: len(x), words)) print(lengths) # [5, 6, 6]
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Filter even numbers evens = list(filter(lambda x: x % 2 == 0, numbers)) print(evens) # [2, 4, 6, 8, 10] # Filter strings longer than 5 chars words = ["cat", "elephant", "dog", "giraffe"] long_words = list(filter(lambda x: len(x) > 5, words)) print(long_words) # ['elephant', 'giraffe']
# Sort by absolute value
numbers = [5, -3, 2, -8, 1, 0, -2]
sorted_nums = sorted(numbers, key=lambda x: abs(x))
print(sorted_nums) # [0, 1, 2, -2, -3, 5, -8]
# Sort list of dicts by key
users = [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25},
{"name": "Charlie", "age": 35}
]
sorted_users = sorted(users, key=lambda x: x["age"])
print([u["name"] for u in sorted_users]) # ['Bob', 'Alice', 'Charlie']from functools import reduce numbers = [1, 2, 3, 4, 5] # Sum all numbers total = reduce(lambda x, y: x + y, numbers) print(total) # 15 # Find maximum maximum = reduce(lambda x, y: x if x > y else y, numbers) print(maximum) # 5
# Lambda with multiple arguments
add = lambda x, y: x + y
print(add(5, 3)) # 8
# Three arguments
combine = lambda a, b, c: f"{a}-{b}-{c}"
print(combine("2024", "01", "15")) # "2024-01-15"
# Default argument
power = lambda x, p=2: x ** p
print(power(3)) # 9 (3^2)
print(power(3, 3)) # 27 (3^3)# Lambda executed immediately (lambda x: x * 2)(5) # Returns 10 # For sorting on the fly pairs = [(1, 'one'), (3, 'three'), (2, 'two')] sorted_pairs = sorted(pairs, key=lambda x: x[0]) print(sorted_pairs) # [(1, 'one'), (2, 'two'), (3, 'three')]