Map, Filter, Reduce
Map, Filter, Reduce
A lambda function can take any number of arguments, but can only have one
expression.
Syntax
lambda arguments : expression
Example
x = lambda a, b : a * b
print(x(5, 6))
Example
x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
Example:
numbers = [1, 2, 3, 4]
squared = list(map(lambda x: x**2, numbers))
print(squared) # Output: [1, 4, 9, 16]
Example:
numbers = [1, 2, 3, 4, 5, 6]
even = list(filter(lambda x: x % 2 == 0, numbers))
print(even) # Output: [2, 4, 6]
Example:
numbers = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, numbers)
print(product) # Output: 24 (1*2*3*4)
🔸 Summary Table:
Function Purpose Output Example