Mastering Python (92 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (26 Blogs)
  • Data Science (20 Blogs)
  • Mastering Python (86 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

Map, Filter and Reduce Functions in Python: All you need to know

Last updated on Nov 26,2019 9.8K Views

52 / 62 Blog from Python Fundamentals

Python provides many built-in functions that are predefined and can be used by the end-user by just calling them. These functions not just ease the work of programmers but also create a standard coding environment. In this article, you will be learning about three such impressive functions namely map(), filter and reduce() in Python.

Before moving ahead, let’s take a look at the contents:

So let’s get started. :)

What are map(), filter() and reduce() functions in Python?

As mentioned earlier, map(), filter() and reduce() are inbuilt functions of Python. These functions enable the functional programming aspect of Python. In functional programming, the arguments passed are the only factors that decide upon the output. These functions can take any other function as a parameter and can be supplied to other functions as parameters as well. So let us now take a deeper look at each of these functions.

The map() 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:

SYNTAX:

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:

EXAMPLE:

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

OUTPUT:

<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:

EXAMPLE:

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

OUTPUT:

<map object at 0x00000284B9AEA940>

[1, 4, 9, 16]

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

EXAMPLE:

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

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

OUTPUT:

<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:

EXAMPLE:

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

OUTPUT:   

(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:

SYNTAX:

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.

EXAMPLE:

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

OUTPUT:

<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:

EXAMPLE:

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

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.

reduce-map reduce filter-edureka

The syntax of this function is as follows:

SYNTAX:

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:

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.

EXAMPLE:

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

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.

EXAMPLE:

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

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.

EXAMPLE:

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

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.  

Got a question for us? Please mention it in the comments section of this “map(), filter() and reduce() Functions in Python” blog and we will get back to you as soon as possible.

To get in-depth knowledge on Python along with its various applications, you can enroll for live Python online training with 24/7 support and lifetime access. 

Upcoming Batches For Data Science with Python Certification Course
Course NameDateDetails
Data Science with Python Certification Course

Class Starts on 21st December,2024

21st December

SAT&SUN (Weekend Batch)
View Details
Data Science with Python Certification Course

Class Starts on 15th February,2025

15th February

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Map, Filter and Reduce Functions in Python: All you need to know

edureka.co