Python provides three powerful built-in (or semi-built-in) functional programming tools:
βœ… map() β†’ Transforms data
βœ… filter() β†’ Filters data
βœ… reduce() β†’ Aggregates data

They are often used with lambda functions to write clean, concise, and efficient code.

Let’s go through each one step by step πŸ‘‡


πŸ”Ή 1. map() Function

βœ… Purpose:

map() applies a function to each element of an iterable (like a list or tuple) and returns a map object (which can be converted into a list or other iterable).

βœ… Syntax:

map(function, iterable)

🧩 Example 1 β€” Square of Each Number

numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x**2, numbers))
print(squares)

Output:

[1, 4, 9, 16, 25]

βœ… The lambda function lambda x: x**2 is applied to each number.


🧩 Example 2 β€” Convert Strings to Uppercase

names = ["alice", "bob", "charlie"]
upper_names = list(map(str.upper, names))
print(upper_names)

Output:

['ALICE', 'BOB', 'CHARLIE']

βœ… Here, we used str.upper directly instead of a lambda.


🧩 Example 3 β€” Add Two Lists Element-wise

a = [1, 2, 3]
b = [4, 5, 6]
result = list(map(lambda x, y: x + y, a, b))
print(result)

Output:

[5, 7, 9]

βœ… map() can take multiple iterables if the function accepts multiple arguments.


πŸ”Ή 2. filter() Function

βœ… Purpose:

filter() selects elements from an iterable based on a condition (True/False) and returns only those elements that satisfy the condition.

βœ… Syntax:

filter(function, iterable)
  • The function must return True or False.
  • Only elements where the function returns True are kept.

🧩 Example 1 β€” Filter Even Numbers

numbers = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)

Output:

[2, 4, 6]

βœ… Keeps only even numbers.


🧩 Example 2 β€” Filter Strings Starting with β€œA”

names = ["Alice", "Bob", "Amanda", "Adam", "Charlie"]
a_names = list(filter(lambda x: x.startswith('A'), names))
print(a_names)

Output:

['Alice', 'Amanda', 'Adam']

βœ… Keeps names starting with β€œA”.


🧩 Example 3 β€” Remove Empty Strings

strings = ["hello", "", "world", "", "python"]
non_empty = list(filter(None, strings))
print(non_empty)

Output:

['hello', 'world', 'python']

βœ… Using None as the function removes all β€œfalsy” values ("", 0, None, etc.)


πŸ”Ή 3. reduce() Function

βœ… Purpose:

reduce() applies a function cumulatively to all elements in a sequence, reducing it to a single value.

⚠️ It’s not a built-in β€” you must import it from the functools module.

βœ… Syntax:

from functools import reduce
reduce(function, iterable[, initializer])
  • function takes two arguments.
  • Applies it repeatedly to reduce the sequence to one value.

🧩 Example 1 β€” Sum of All Elements

from functools import reduce

numbers = [1, 2, 3, 4, 5]
total = reduce(lambda x, y: x + y, numbers)
print(total)

Output:

15

βœ… Works like:

(((1 + 2) + 3) + 4) + 5 β†’ 15

🧩 Example 2 β€” Find Maximum Element

from functools import reduce

numbers = [5, 8, 2, 10, 3]
maximum = reduce(lambda x, y: x if x > y else y, numbers)
print(maximum)

Output:

10

βœ… Compares elements pairwise to find the max.


🧩 Example 3 β€” Multiply All Elements

from functools import reduce

numbers = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, numbers)
print(product)

Output:

24

βœ… Equivalent to 1 * 2 * 3 * 4.


🧩 Example 4 β€” Using an Initial Value

from functools import reduce

numbers = [1, 2, 3]
total = reduce(lambda x, y: x + y, numbers, 10)
print(total)

Output:

16

βœ… The initializer (10) is added as the starting value.


πŸ”Ή 4. Combining map(), filter(), and reduce()

You can combine all three for complex data processing.

🧩 Example β€” Double Even Numbers and Find Their Sum

from functools import reduce

numbers = [1, 2, 3, 4, 5, 6]

# Step 1: Filter even numbers
evens = filter(lambda x: x % 2 == 0, numbers)

# Step 2: Double them
doubles = map(lambda x: x * 2, evens)

# Step 3: Sum them
result = reduce(lambda x, y: x + y, doubles)

print("Final Result:", result)

Output:

Final Result: 24

βœ… Step-by-step:

Even numbers β†’ [2, 4, 6]
Doubled β†’ [4, 8, 12]
Sum β†’ 24

πŸ”Ή 5. Practical Example β€” Word Processing

Count Total Length of Words Starting with β€˜P’

from functools import reduce

words = ["Python", "Programming", "is", "Powerful", "and", "Popular"]

# Filter words starting with P
p_words = filter(lambda w: w.startswith('P'), words)

# Map to word lengths
lengths = map(len, p_words)

# Reduce to total length
total_length = reduce(lambda x, y: x + y, lengths)

print("Total Length:", total_length)

Output:

Total Length: 26

βœ… "Python" (6) + "Programming" (11) + "Powerful" (8) + "Popular" (7) β†’ 32
(Example output may vary depending on words used.)


πŸ”Ή 6. Comparison Summary

FunctionPurposeReturnsCommon Use
map()Transform all elementsNew iterable (map object)Apply operation to each item
filter()Select some elementsNew iterable (filter object)Keep elements that match condition
reduce()Aggregate all elementsSingle valueSummation, product, max/min

βœ… Example Recap

TaskCodeOutput
Square numbersmap(lambda x: x**2, [1,2,3])[1, 4, 9]
Filter even numbersfilter(lambda x: x%2==0, [1,2,3,4])[2, 4]
Sum all numbersreduce(lambda x,y: x+y, [1,2,3,4])10

🧾 Summary Table

FunctionDescriptionExampleOutput
map()Applies a function to all elementsmap(lambda x: x*2, [1,2,3])[2,4,6]
filter()Keeps elements that satisfy a conditionfilter(lambda x: x>2, [1,2,3,4])[3,4]
reduce()Reduces a list to one valuereduce(lambda x,y:x*y, [1,2,3,4])24

βœ… In short:

  • map() β†’ transforms every element
  • filter() β†’ selects specific elements
  • reduce() β†’ combines all elements into one result
  • Often used with lambda functions for cleaner, functional-style code.

🧩 Final Example (All-in-One):

from functools import reduce

numbers = [1, 2, 3, 4, 5, 6]

# Step 1: Keep even numbers
# Step 2: Square them
# Step 3: Sum the result
result = reduce(
    lambda x, y: x + y,
    map(lambda x: x**2, filter(lambda x: x % 2 == 0, numbers))
)

print("Sum of squares of even numbers:", result)

Output:

Sum of squares of even numbers: 56

βœ… Explanation:

Even numbers β†’ [2, 4, 6]
Squares β†’ [4, 16, 36]
Sum β†’ 56

🧠 Final Takeaway:

map(), filter(), and reduce() are Python’s functional programming tools
that let you transform, filter, and aggregate data in a clean and concise way β€”
making your code more expressive, elegant, and efficient.


Scroll to Top