Map Filter Reduce
Map Filter Reduce
🔹 1. map() Function
Purpose: Applies a function to each item in an iterable and returns a map object (which
can be converted to a list, tuple, etc.).
Syntax:
map(function, iterable)
Example:
def square(x):
return x * x
numbers = [1, 2, 3, 4]
print(squared)
Output:
[1, 4, 9, 16]
numbers = [1, 2, 3]
print(result)
Output:
🔹 2. filter() Function
Purpose: Filters items from an iterable where the function returns True.
Syntax:
filter(function, iterable)
Example:
def is_even(x):
return x % 2 == 0
numbers = [1, 2, 3, 4, 5]
print(evens)
Output:
[2, 4]
print(result)
Output:
[20, 25]
🔹 3. reduce() Function
Purpose: Applies a function cumulatively to the items of an iterable (i.e., reduces it to a
single value).
Syntax:
reduce(function, iterable)
Example:
return x * y
numbers = [1, 2, 3, 4]
print(result)
Output:
24
print(result)
Output:
10
📊 Comparison Table
Function Purpose Returns Typical Use Case