Open In App

Map Reduce and Filter Operations in Python

Last Updated : 16 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will study Map, Reduce, and Filter Operations in Python. These three operations are paradigms of functional programming. They allow one to write simpler, shorter code without needing to bother about intricacies like loops and branching. In this article, we will see Map Reduce and Filter Operations in Python.

Map Reduce and Filter Operations in Python

Below, are examples of Map Reduce and Filter Operations in Python:

  • map() Function
  • Reduce() Function
  • Filter() Function

Map Function in Python

The map () function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple, etc.).

Syntax: map(fun, iter)

Parameters:

  • fun: It is a function to which map passes each element of given iterable.
  • iter: iterable object to be mapped.

Example: In this example, Python program showcases the usage of the map function to double each number in a given list by applying the double function to each element, and then printing the result as a list.

Python3
# Function to return double of n
def double(n):
    return n * 2

# Using map to double all numbers
numbers = [5, 6, 7, 8]
result = map(double, numbers)
print(list(result))

Output
[10, 12, 14, 16]

Reduce Function in Python

The reduce function is used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along.This function is defined in “functools” module.

Syntax: reduce(func, iterable[, initial])

Parameters:

  • fun: It is a function to execuate on each element of the iterable objec
  • iter: It is iterable to be reduced

Example : In this example, we are using reduce() function from the functools module to compute the product of elements in a given list by continuously applying the lambda function that multiplies two numbers together, resulting in the final product.

Python3
import functools

# Define a list of numbers
numbers = [1, 2, 3, 4]

# Use reduce to compute the product of list elements
product = functools.reduce(lambda x, y: x * y, numbers)
print("Product of list elements:", product)

Output
Product of list elements: 24

Filter Function in Python

The filter() method filters the given sequence with the help of a function that tests each element in the sequence to be true or not. 

Syntax: filter(function, sequence)

Parameters:

  • function: function that tests if each element of a sequence is true or not.
  • sequence: sequence which needs to be filtered, it can be sets, lists, tuples, or containers of any iterators.

Example : In this example, we defines a function is_even to check whether a number is even or not. Then, it applies the filter() function to a list of numbers to extract only the even numbers, resulting in a list containing only the even elements. Finally, it prints the list of even numbers.

Python3
# Define a function to check if a number is even
def is_even(n):
    return n % 2 == 0

# Define a list of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Use filter to filter out even numbers
even_numbers = filter(is_even, numbers)
print("Even numbers:", list(even_numbers))  

Output
Even numbers: [2, 4, 6, 8, 10]

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads