Python Map, Filter and Reduce functions

Python Map, Filter and Reduce functions


These are three functions which facilitate a functional approach to programming. We will discuss them one by one and understand their use cases.

1. Python Map function


The map() function applies a given function to each item of an iterable (list, tuple etc.) and returns a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted.

map(function, iterable, ...)
  • function - map() passes each item of the iterable to this function.
  • iterable - iterable which is to be mapped

Map Function

You can pass more than one iterable to the map() function.

Here is example:

map(function_to_apply, list_of_inputs)

Most of the times we want to pass all the list elements to a function one-by-one and then collect the output. For instance:

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

# output: [1, 4, 9, 16, 25]

Map allows us to implement this in a much simpler and nicer way. Here you go:

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

# output: [1, 4, 9, 16, 25]

Most of the times we use lambdas with map so I did the same. Instead of a list of inputs we can even have a list of functions!

def multiply(x):
    return (x*x)
def add(x):
    return (x+x)

funcs = [multiply, add]
for i in range(5):
    value = list(map(lambda x: x(i), funcs))
    print(value)

# Output:
# [0, 0]
# [1, 2]
# [4, 4]
# [9, 6]
# [16, 8]

Another example:

list('sat')
# Output: ['s', 'a', 't']

words = ['sat', 'bat', 'cat', 'mat']
list(map(list, words))

# Output: [['s', 'a', 't'], ['b', 'a', 't'], ['c', 'a', 't'], ['m', 'a', 't']]

Passing more than one iterable to map() function


If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted.

list1 = [4, 5, 6]
list2 = [5, 6, 7]
list(map(lambda n1, n2: n1+n2, list1, list2))

# Output: [9, 11, 13]

Map Function

With multiple iterables, the iterator stops when the shortest iterable is exhausted.

list1 = [4, 5, 6, 7, 8, 9]
list2 = [5, 6, 7]
list(map(lambda n1, n2: n1+n2, list1, list2))

# Output: [9, 11, 13]

2. Python Filter function


The filter() function takes a function and a sequence as arguments and returns an iterable, only yielding the items in sequence for which function returns True. If None is passed instead of a function, all the items of the sequence which evaluates to False are removed. The syntax of the filter() is as follows:

syntax: filter(function or None, iterable) –> filter object

Here is an example:

Python3

>>> 
>>> def is_even(x):
...     if x % 2 == 0:
...         return True
...     else:
...         return False
... 
>>> 
>>> f = filter(is_even, [1, 3, 10, 45, 6, 50])
>>>
>>> f
<filter object at 0x7fcd88d54eb8>
>>> 
>>> 
>>> for i in f:
...     print(i)
... 
10
6
50
>>>

To produce the result at once we can use the list() function.

Python3

>>> list(filter(is_even, [1, 3, 10, 45, 6, 50]))
[10, 6, 50]
>>> 
>>> list(filter(None, [1, 45, "", 6, 50, 0, {}, False])) # function argument is None
[1, 45, 6, 50]
>>>

In Python 2, filter() returns an actual list (which is not the efficient way to handle large data), so you don’t need to wrap filter() in a list() call.

Python2

>>> 
>>> filter(is_even, [1, 3, 10, 45, 6, 50])
[10, 6, 50]
>>>

Here are some other examples.

Python3

>>> 
>>> filter(lambda x: x % 2 != 0, [1, 3, 10, 45, 6, 50]) # lambda is used in place of a function
[1, 3, 45]
>>>
>>> 
>>> list(filter(bool, [10, "", "py"]))
[10, 'py']
>>> 
>>>
>>> import os
>>> 
>>> list(filter(lambda x: x.startswith(".") != True, os.listdir(".") )) # display all files in the current directory (except the hidden ones)
['Documents', 'Downloads', 'Desktop', 'Pictures', 'bin', 'opt', 'Templates', 'Public', 'Videos', 'Music']
>>>

Filter Function

As the name suggests, filter function creates a list of elements for which a function returns true.

Here is a short and concise example:

number_list = range(-5, 5)
less_than_zero = list(filter(lambda x: x < 0, number_list))
print(less_than_zero)

# Output: [-5, -4, -3, -2, -1]

The filter resembles a for loop but it is a builtin function and faster.

Note: If map & filter do not appear beautiful to you then you can read about list/dict/tuple comprehensions.

3. Python Reduce function


The reduce() function accepts a function and a sequence and returns a single value calculated as follows:

  • Initially, the function is called with the first two items from the sequence and the result is returned.

  • The function is then called again with the result obtained in step 1 and the next value in the sequence. This process keeps repeating until there are items in the sequence. e.g in step 2 it will be 3th item in the sequence, in step 3 it will be 4th item in sequence.

The syntax of the reduce() function is as follows:

syntax: reduce(function, sequence[, initial]) -> value

When the initial value is provided, the function is called with the initial value and the first item from the sequence.

In Python 2, reduce() was a built-in function. However, in Python 3, it is moved to functools module. Therefore to use it, you have to first import it as follows:

from functools import reduce # only in Python 3

Reduce Function

Reduce is a really useful function for performing some computation on a list and returning the result. It applies a rolling computation to sequential pairs of values in a list.

Here is an example which adds all the items in the list:

from functools import reduce

def do_sum(x1, x2): return x1 + x2

reduce(do_sum, [1, 2, 3, 4])
# output: 10

This reduce() call perform the following operation:

(((1 + 2) + 3) + 4) => 10

For example, if you wanted to compute the product of a list of integers. So the normal way you might go about doing this task in python is using a basic for loop:

product = 1
list = [1, 2, 3, 4]
for num in list:
    product = product * num

# product = 24

Now let’s try it with reduce:

from functools import reduce
product = reduce((lambda x, y: x * y), [1, 2, 3, 4])
print(product)

# Output: 24

source:

http://book.pythontips.com/en/latest/map_filter.html

https://thepythonguru.com/python-builtin-functions/reduce/

Share Comments
comments powered by Disqus