Get Informed out of Data

Full width home advertisement



Post Page Advertisement [Top]

Functional Programming in Python - map,filter,reduce

Functional Programming in Python - map,filter,reduce
Python has been language of consistency with respect to updates and adding features. Map,filter,reduce are previous featured in the java script language but, python 3 now has these features too.

Functional programming languages are specially designed to handle symbolic computation and list processing applications.

Functional programming is based on mathematical functions. Some of the popular functional programming languages include: Lisp, Python, Erlang, Haskell, Clojure, etc.

Some of the Characteristic features of Functional Programming:

  • designed on the concept of mathematical functions that use conditional expressions and recursion to perform computation.
  • supports higher-order functions and lazy evaluation features.
  • don’t support flow Controls like loop statements and conditional statements like If-Else and Switch Statements.
  • supports  Abstraction, Encapsulation, Inheritance, and Polymorphism.

In such a way, python3 is using it as an method to call for certain feature.especially working on large dataset.

Python has three special functions lets see them in detail:

It can be seen as a something we often doing with one method which can be made as a function.


The map() function is a type of higher-order. As mentioned earlier, this function takes another function as a parameter along with a sequence of iterables and returns an output after applying the function to each iterable present in the sequence. Its syntax is as follows:


map(function, iterables)  

Here, the function defines an expression that is in turn applied to the iterables. The map function can take user-defined functions as well as lambda functions as a parameter.

Using user-defined and Lambda Functions within:

User-defined functions within map(): 

The map() function can take user-defined functions as parameters. The parameters of these functions are exclusively set by the user or the programmer. For example:


def newfunc(a):
    return a*a
x = map(newfunc, (1,2,3,4)) #x is the map object


<map object at 0x00000284B9AEADD8>

{16, 1, 4, 9}

As you can see, x is a map object. The next part output displays the map function taking newfunc() as its parameter and then it applies the a*a to all the iterables. As a result, the values of all iterables are multiplied by themselves and returned.

NOTE: The output is not in order of the values of the iterables because I have used the set() function. You can also use the list() or tuple() functions for example:


def newfunc(a):
    return a*a
x = map(newfunc, (1,2,3,4)) #x is the map object


<map object at 0x00000284B9AEA940>

[1, 4, 9, 16]

You can pass more than one list of parameters as well. For example:


def func(a, b):
    return a + b

a = map(func, [2, 4, 5], [1,2,3])


<map object at 0x00000284B9BA1E80>

(3, 6, 8)

Now let us see how you can use lambda functions within the map() function.

Lambda functions within map():

Lambda functions are functions that do have any name. These functions are often supplied as parameters to other functions. Now let us try to embed lambda functions within the map() function. Consider the following example:


tup= (5, 7, 22, 97, 54, 62, 77, 23, 73, 61)
newtuple = tuple(map(lambda x: x+3 , tup)) 


(8, 10, 25, 100, 57, 65, 80, 26, 76, 64)

The above output is a result of applying the lambda expression (x+3) to each item present in the tuple.

The filter() function:

The filter() function is used to create an output list consisting of values for which the function returns true. The syntax of it is as follows:


filter(function, iterables)

Just like map(), this function can be used can also take user-defined functions as well as lambda functions as a parameter.


def func(x):
    if x>=3:
        return x
y = filter(func, (1,2,3,4))  


<filter object at 0x00000284B9BBCC50>

[3, 4]

As you can see, y is the filter object and the list is a list of values that are true for the condition (x>=3).

Using lambda within filter():

The lambda function that is used as a parameter actually defines the condition that is to be checked. For example:


y = filter(lambda x: (x>=3), (1,2,3,4))

OUTPUT:   [3, 4]

The above code produces the same output as the previous function.

The reduce() function:

The reduce() function, as the name describes, applies a given function to the iterables and returns a single value.

The syntax of this function is as follows:

The reduce() function:


reduce(function, iterables)

The function here defines what expression needs to be applied to the iterables. This function needs to be imported from the functools module. For Example:


from functools import reduce
reduce(lambda a,b: a+b,[23,21,45,98])

OUTPUT:  187

In the above example, the reduce function consecutively adds each iterable present in the list and returns a single output.

The map(), filter() and reduce() functions in Python can be used along with each other.
Using map(),filter() and reduce() functions along with each other:

When you do this, the internal functions are first solved and then the outer functions operate on the output of the internal functions.

Let us first try passing the filter() function as a parameter to the map() function.

Using filter() within map():

The code given below first checks for the condition (x>=3) to be true for the iterables. Then, the output is mapped using the map() function.


c = map(lambda x:x+x,filter(lambda x: (x>=3), (1,2,3,4)))

OUTPUT:   [6, 8]

If you filter out integers greater than or equal to 3 from the given tuple, you get [3,4] as the result. Then if you map this using(x+x) condition, you will get [6,8], which is the output.

Using map() within filter():

When you use the map() function within filter() function, the iterables are first operated upon by the map function and then the condition of filter() is applied to them.


c = filter(lambda x: (x>=3),map(lambda x:x+x, (1,2,3,4))) #lambda x: (x>=3)

OUTPUT:   [4, 6, 8]

Using map() and filter() within reduce():

The output of the internal functions is reduced according to the condition supplied to the reduce() function.


d = reduce(lambda x,y: x+y,map(lambda x:x+x,filter(lambda x: (x>=3), (1,2,3,4)))) 

OUTPUT:    14

The output is a result of [6,8] which is the result of the internal map() and filter() functions.

With this, we have reached the end of this article on map(), filter() and reduce functions in Python. I hope you have understood everything clearly. Make sure you practice as much as possible and revert your experience.


Bottom Ad [Post Page]