Filter Map Reduce Functions in Python

In this class, we discuss Filter Map Reduce Functions in Python.

For Complete YouTube Video: Click Here

Filter Map Reduce Functions

The reader should have prior knowledge of functions. Click here.

Filter Function

Let’s take an example and understand the filter function.

The filter function will take two input arguments. 

The first argument is a function object. And the second argument should be an iterate object-like list.

ages = [5, 12, 17, 18, 24, 32]

def myFunc(x):
  if x < 18:
    return False
  else:
    return True
adults = filter(myFunc, ages)
for x in adults:
  print(x)

Output:
18
24
32

In the above program, the ages variable is a list containing ages. And we define a function MyFunc.

The function MyFunc and ages are sent as arguments to the filter function.

What will the filter function do?

The filter function will take elements in the list one after another and send the element as input to MyFunc.

If the function MyFunc returns true. Then filter function will return that element. Otherwise, discard the element.

In our function MyFunc. Will return True if age is greater than 18.

The filter function will return those elements greater than 18.

Map Function

Take an example and understand the map function.

The map function takes two arguments as input.

The first argument should be the function object. And the second argument should be an iterate object-like list.

def square(x):
    return x*x
a=[1,2,3,4,5,6]
z= map(square,a)
for i in z:
    print(i)

Output:
1
4
9
16
25
36

In the above example, we have written a function square. And a list a.

The function square and list a send as arguments to map function.

The map function will take elements from the list one after another and send the element as input to the function square.

The value return by the square function is return by the map function.

Reduce Function

The reduce function is in the module functools.

We will discuss the concept of the module in our coming classes.

import functools as f
def summation(x,y):
    return x+y
a=[1,2,3,4,5,6]
z=f.reduce(summation,a)
print(z)

Output:
21

The Reduce function will take two arguments as input parameters.

The first argument should be a function object, and the second argument should be an iterate object-like list.

The reduce function will take elements from the list and give them as input to the function.

The first time the reduce function will take two elements from the list. And send them as input to the function.

Later on, the reduce function will take one element from the list, and the result obtained from the previous function call is taken as a second input.

In our example, a=[1,2,3,4,5,6,7,8,9]

Function summation will add the values given to the function.

First-time element one and element two are given as input to the summation function.

Summation function will return value 3.

Next time value three is taken as the first input, and element three from the list is taken as a second input to the function summation.

The reduce function will continue by taking elements one after the other from the list.

In our example, summation of all the elements in the list is our output.