Higher Order function in Python

Map, Filter and Reduce

Posted by Anuprabha on 09-Mar-2022

Hi Pythonistas,

Today we are going to learn about 3 higher order function in Python they are map, filter and reduce.

Functions that can take other functions as parameters or return a function as output as called Higher order functions.

map

Syntax:

map(function, iterables)

The map() function iterates through all items in the iterable and executes the function on each of them. The iterable could be a list, tuple, or any iterable object. 

Example

Suppose we want to double each element in the list. Usually what we would do is,

items = [1, 2, 3, 4, 5]
doubled_items = []
for i in items:
    doubled_items.append(2*i)
print(doubled_items)

output:

[2, 4, 6, 8, 10]

Now let’s try this using map:

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

def doubled(n):
    return n*2

doubled_items = map(doubled, items)

print(doubled_items)

Output:

<map object at 0x7f3c2080aca0>

The output of a map function is a map object, which is an iterator. If we need the result as a list, we can use the built-in list() function.

doubled_list = list(doubled_items)
print(doubled_list)

output:

[2, 4, 6, 8, 10]

We can make this even simpler by using lambda expression instead of the normal doubled() function,

Using lambda function

items = [1, 2, 3, 4, 5]
doubled_list = list(map(lambda x: x*2, items))
print(doubled_list)

output:

[2, 4, 6, 8, 10]

We got the same output using a single line of code!

To learn more about lambda function please click here

filter

filter takes in a function and an iterable as arguments. However unlike map, only one iterable is required.

Syntax:

filter(function, iterable)

filter() passes each element in the iterable through a function and returns only the elements that evaluate to true.

The function argument has to return a Boolean value, either True or False.

Here's an example where we fetch the even numbers from a list

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

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

even_numbers = filter(is_even, items)

print(even_numbers)

Output:

<filter object at 0x7f3c2080ad00>

The output of filter() is a filter object which is an iterator

Using Lambda

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

even_numbers = list(filter(lambda x: x%2 == 0, items))

reduce()

Continually applies a function to the iterable and returns a single value.

Syntax:

reduce(function, iterable[, initial])

reduce() takes a function, an iterable, and an optional initial as arguments. The function argument requires two parameters.

In Python 3+ unlike map() and filter() which are built-in functions, reduce() has to be imported from the functools module. 

 Let's see an example:

 

#Multiply numbers in a list

 from functools import reduce

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

result = reduce(lambda x,y : x*y, items)

print(result)

 output:

 120

Note that the output of reduce() is a single value and not an iterator.

The reduce() first applies the function to the first two elements of the iterable, then it will apply the function to the output value and the third element in the iterable, and so on. Once it reaches the last element, it will return the final value.

Now let's see what happens when we use the optional initial argument.

With initial argument

from functools import reduce

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

result = reduce(lambda x,y : x*y, items, 2)

print(result)

 output:

240

 If initial is given, then it becomes the first argument to function and the first element in iterable becomes the second element, and so on.

Now that you have seen map, filter, and reduce functions you might be wondering why we should use these instead of loops They are

  • Simple and less number of codes
  • Performance improvement