Python’s Map, Reduce, and Filter: The Magic of Functional Programming

Samiul Sk
3 min readJul 26, 2023

--

Functional programming is a powerful paradigm that can help you write more concise, readable, and maintainable code. In Python, the map, reduce, and filter functions are three of the most important tools for functional programming.

Map

The map function takes a function and an iterable as input and returns a new iterable where the function has been applied to each element of the original iterable. For example, the following code uses the map function to square all the numbers in a list:

# Without map: Traditional loop
numbers = [1, 2, 3, 4, 5]
squared_numbers = []
for num in numbers:
squared_numbers.append(num ** 2)

# value of squared_numbers now: [1, 4, 9, 16, 25]
# with the map function 
numbers = [1, 2, 3, 4, 5]
squared_numbers = map(lambda x: x ** 2, numbers)

# numbers are squared now [1, 4, 9, 16, 25]
# just use list(squared_numbers) as currently it's an iterable

Reduce

The reduce function takes a function and an iterable as input and returns a single value.

The function is applied to the first two elements of the iterable, and the result is then applied to the next element, and so on. This process continues until all the elements of the iterable have been processed. For example, the following code uses the reduce function to sum all the numbers in a list:

# Without reduce 
numbers = [1, 2, 3, 4, 5]
sum_numbers = 0

# Without reduce: Using a for loop
for num in numbers:
sum_numbers += num

print(sum_numbers) # Output: 15
# with reduce function 

from functools import reduce

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

sum_numbers = reduce(lambda x, y: x + y, numbers)

print(sum_numbers)

Filter

The filter function takes a function and an iterable as input and returns a new iterable where the elements that satisfy the condition specified by the function are included. For example, the following code uses the filter function to filter out all the even numbers from a list:

# Without filter Using a for loop and if
numbers = [1, 2, 3, 4, 5]
even_numbers = []

for num in numbers:
if num % 2 == 0:
even_numbers.append(num)

print(even_numbers) # Output: [2, 4]
numbers = [1, 2, 3, 4, 5]

# using the filter, that's all we need
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))

print(even_numbers)

The Power of Function Composition

The map, reduce, and filter functions can be combined to create powerful pipelines that can perform complex tasks. For example, the following code uses the map, reduce, and filter functions to count the number of words in a string that are longer than 5 characters:

string = "This is a long string that contains more than 5 characters."

word_counts = list(filter(lambda word: len(word) > 5,
map(lambda word: word,
string.split(" "))))

print(word_counts) #

Real-World Applications

The map, reduce, and filter functions can be used in a wide variety of real-world applications. For example, they can be used to:

  • Data analysis
  • String manipulation
  • Visualization
  • Machine learning
  • Web development

Negative aspects:

  • Can be difficult to understand for beginners. The map, reduce, and filter functions can be difficult to understand for beginners, especially if they are not familiar with functional programming.
  • Can be less efficient than loops in some cases. In some cases, the map, reduce, and filter functions can be less efficient than loops. This is because the map, reduce, and filter functions have to create a new iterable for each element in the original iterable.

--

--