Python filter, map and reduce

Python’s `filter`, `map`, and `reduce` functions are higher-order functions that enable powerful transformations and data filtering on iterables like lists. While `filter` and `map` are standard functions in Python, `reduce` is part of the `functools` module and requires importing. These functions allow efficient functional programming patterns, particularly useful in processing large data sets.

1. `filter`: Filtering Data Based on Conditions

The `filter` function applies a filtering function to each item in an iterable, returning only the items that satisfy a given condition. It takes two arguments:

- `filter(function, iterable)`
- `function`: A function that returns a boolean (True or False).
- `iterable`: The collection (e.g., list, tuple) to filter.

Basic Example of `filter`:
# Filtering out even numbers from a list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

def is_even(num):
    return num % 2 == 0

even_numbers = list(filter(is_even, numbers))
print("Filtered even numbers:", even_numbers)

Output:
Filtered even numbers: [2, 4, 6, 8]

Explanation: Here, `filter(is_even, numbers)` applies `is_even` to each item in `numbers`, returning only even numbers.

Advanced Use of `filter` with Lambda Functions

We can use lambda functions directly within `filter`, eliminating the need to define separate functions.
# Filtering out numbers greater than 5 using a lambda function
numbers = [1, 2, 3, 6, 7, 8]

filtered_numbers = list(filter(lambda x: x > 5, numbers))
print("Numbers greater than 5:", filtered_numbers)

Output:
Numbers greater than 5: [6, 7, 8]

Explanation: This filter operation directly uses `lambda x: x > 5`, filtering numbers greater than 5 without defining a separate function.

2. `map`: Applying a Function to All Items

The `map` function applies a specified function to each item in an iterable, returning a map object containing results of the applied function. It takes two arguments:

- `map(function, iterable)`
- `function`: A function that operates on each item.
- `iterable`: The collection to process.

Basic Example of `map`:
# Squaring each number in a list
numbers = [1, 2, 3, 4, 5]

def square(num):
    return num ** 2

squared_numbers = list(map(square, numbers))
print("Squared numbers:", squared_numbers)

Output:
Squared numbers: [1, 4, 9, 16, 25]

Explanation: The `map(square, numbers)` function applies `square` to each item in `numbers`, returning squared values.

Advanced Use of `map` with Lambda Functions and Multiple Iterables

We can combine `map` with lambda functions or use multiple iterables in `map` for element-wise operations.
# Adding corresponding elements from two lists
numbers1 = [1, 2, 3]
numbers2 = [4, 5, 6]

added_numbers = list(map(lambda x, y: x + y, numbers1, numbers2))
print("Element-wise sum:", added_numbers)

Output:
Element-wise sum: [5, 7, 9]

Explanation: Using `map(lambda x, y: x + y, numbers1, numbers2)`, we add each corresponding pair of elements from `numbers1` and `numbers2`.

3. `reduce`: Accumulating Results

The `reduce` function, part of the `functools` module, applies a function cumulatively to items of an iterable, reducing it to a single value. It takes two arguments:

- `reduce(function, iterable)`
- `function`: A function that operates on two items, producing one item.
- `iterable`: The collection to reduce.

Basic Example of `reduce`:
from functools import reduce

# Calculating the product of all numbers in a list
numbers = [1, 2, 3, 4]

def multiply(x, y):
    return x * y

product = reduce(multiply, numbers)
print("Product of numbers:", product)

Output:
Product of numbers: 24

Explanation: `reduce(multiply, numbers)` applies `multiply` across all items, returning a single product of all numbers.

Advanced Use of `reduce` with Lambda Functions

We can combine `reduce` with lambda functions for quick, inline accumulation operations.
from functools import reduce

# Summing up all numbers in a list using lambda
numbers = [10, 20, 30, 40]

total_sum = reduce(lambda x, y: x + y, numbers)
print("Sum of numbers:", total_sum)

Output:
Sum of numbers: 100

Explanation: This example sums all numbers in `numbers` using a lambda function with `reduce`.

4. Combining `filter`, `map`, and `reduce`

These functions can be combined to perform complex transformations and computations efficiently.
from functools import reduce

# Filtering, mapping, and reducing in one expression
numbers = [1, 2, 3, 4, 5, 6]

# Filter even numbers, then square them, then sum them
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: This example:
1. Filters even numbers (`filter(lambda x: x % 2 == 0, numbers)`).
2. Maps to square each even number (`map(lambda x: x ** 2, ...)`)
3. Reduces to sum these squares (`reduce(lambda x, y: x + y, ...)`).

5. Summary and Use Cases

These functional tools offer efficient ways to process and transform data:

- `filter`: For extracting data based on conditions (e.g., removing outliers, filtering text).
- `map`: For transforming data uniformly (e.g., unit conversion, normalization).
- `reduce`: For aggregation operations (e.g., summing totals, finding products).

Using these functions, particularly with lambda expressions, offers concise and readable solutions for data processing tasks, especially when combined with Python's functional programming capabilities.

Previous: Python Iterators and Generators | Next: Python Classes

<
>