Built-in Python functions map( ), filter( ) & reduce( ) , you should literally learn right now.

There is no point how powerful any language is if you don’t know how to use its features

Techdobz
Quick Code
5 min readFeb 21, 2022

--

These functions are commonly used in functional-style python programming.

These functions are most often used in conjunction with the anonymous ( lambda ) function, but you can use it with a normal function which defines by ( def ) key word.

map( ) , reduce( ) and filter( ) all were available in python 2 , but in python 3 reduce( ) function was moved to functools module.

Lambda ( anonymous ) Function

Let’s first understand what is an anonymous function in python is,

This function starts with keyword lambda and has arguments and expressions both separated by ( : )

lambda arguments : expression

Ex :

This will be same as

As you can see lambda function does not require any name that’s why it is called an anonymous function.

We use this function when we require a nameless function and in the case of functional programming.

One more thing I should mention is what is an iterable ?

iterable is any object which can be iterated using for loop,

ex: list, string, dictionary, tuple

map ( )

Syntax : map(function, iterable, . . . )

Here,
the function will be performed on each element of iterable.

Also, you should know that map() will return an iterator, not a particular value, so its value can be used whenever requires.

You can pass more than one iterable in the map function, but the important thing to remember is in case of different sizes of the iterable map will stop when it consumes the shortest iterable.

Ex: let’s find square of every element from list.

All this function does the same thing it will return the square of 1 to 10 number, but as you know it will return an iterator it will be something like this,

But you can pass map() in list() function or use for loop to see result,

Accessing element using list( )
Accessing element using for loop

map( ) [ with multiple iterable ]

Second time see it is stop at the smallest iterable which is range(4).

filter( )

Syntax : filter(function,iterable, . . . )

Unlike map(), the filter() function will not transform the input value into a new value, but it will return a value that matches the condition mentioned in the function.

As the name suggests it will filter out elements from iterable based on certain conditions.

Ex : let’s find odd and even numbers from a list of elements.

reduce( )

Syntax : reduce(function, iterable)

As map() function will perform function on each element of iterable one by one,
In reduce() this function will cumulatively perform operations specify by the function,
This function will return an only single value,

Also, we have to import it from the functools module in python3

Let’s see how it works,

Let’s understand what just happen,
This will,
Starts at a, b which will be 1, 2
performs a + b which will be 3
Now a, b will be the result of the previous a + b which is 3, and next element in the list
Let’s understand it better in the below image

Now, map() and filter() functions returns an iterator which you access by loop or list() function which is also a loop inside,

but also you can access it’s element by next() function like this,

In this article, I have explored the very powerful and useful built-in function of python,

Map( )
→ map(function,iterable, . . . )
→ Perform function on each element of an iterable
→ Returns an iterator
→ Generate a new value for each element of an iterable
→In the case of multiple iterable, it will stop at the exhaustion of the smallest iterable.

Filter( )
→filter(function,iterable, . . . )
→Perform function on each element of an iterable
→Returns an iterator
→Returns Values that meet certain criteria defined by the function

Reduce( )
→reduce(function,iterable)
→Cumulatively performs operations specify by the function
→Return single value

Let’s see how much things get complicated if you decide that you don’t want to use this built-in function or maybe you don’t know about that.
An Example with and without the use of a map

Ex :

4521369741 → you have find sum of this numbers until it’s an single digit number.
Like this : 4 + 5 +2 +1 + 3 + 6 + 9 + 7+ 4 + 1 = 42 > 4 + 2 = 6 ( output )

without use of map( )
with use of map ( )
output in both cases

you know what, I have written that code by myself when I don’t know about this fuction exists but now I know this function and you too.

“If you have an easy way around things that will give the same result, don’t waste your time to build the different way, it will just waste your time and energy.”

But It might be possible that you don’t know that an easy way exists for certain problems, but if you explore enough you will eventually find one.

KEEP LEARNING & KEEP GOING

--

--

Techdobz
Quick Code

I write about programming , self awareness , lessons , productivity that help me live a better life. https://techdobz.herokuapp.com