Python map, filter, and reduce for Data Processing

What is map, filter, and reduce?

Python can also be used with functional programming with functions such as map, filter, and reduce which helps in efficient transformations, filtering, and aggregations on data collections.

  • map applies a specified function to every item in an iterable (like a list) and returns the results.
  • filter removes items from a collection that don’t meet a specified condition.
  • reduce aggregates all items into a single result based on an operation.

Why use map, filter, and reduce?

These functions enable efficient data manipulation and make code more concise. They’re helpful in tasks like applying adjustments to a list of barangay donations or selecting donations above a certain amount. By using map, filter, and reduce you can perform these actions in one line, improving readability and functionality.

Syntax

map(function, iterable)
filter(function, iterable)
reduce(function, iterable)

Example

Let’s say we want to filter, increase, and calculate the donations into a barangay we can do it using map, filter, and reduce.

# Import reduce from functools
from functools import reduce

donatios = [1000, 1500, 500, 2000, 2500]

# Increase each donation by 10% using map
updated_donatios = list(map(lambda x: x*1.1,donations))

# Filter donations abouve Php 2000
large_donations = list(filter(lambda x : x > 2000, updated_donations))

# Calculate the total donation using reduce
total_donation = reduce(lambda x, y: x + y, updated_donations)

print("Updated Donations:", updated_donations)  # Updated amounts
print("Large Donations:", large_donations)      # Donations above 2000
print("Total Donation:", total_donation)        # Sum of all donations

Scroll to Top