DS - Map, Reduce Filter Functions

Suppose we take a list:

l = [2,3,4,5,6]

Suppose we create a function to find square of list and call it and get output:

def test(l): l1 = [] for i in l: l1.append(i**2) return l1

test(l)

o/p: [4, 9, 16, 25, 36]

Now we want to map every element of list with its square. So that we get result in a while. So we use map() function.

In map(), we can write external function also and lambda function also.

Now we create function sq for getting square of number:

def sq(x): return x**2

then we map it with list l:

o/p: <map at 0x7fc889717760>

we wrap the output in list:

o/p: [4, 9, 16, 25, 36]

map() takes 2 parameters, function & iterable

now, we try with lambda function:

list(map(lambda x:x**2,l))

o/p: [4, 9, 16, 25, 36]

Suppose we take 2 lists l1 and l2 and we want result list as addition of individual elements position wise, eg. sum of 1st element of l1 with 1st element of l2 then 2nd element of l1 with 2nd element of l2.... ; for that we have used lambda function:

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

l2 = [6, 7, 8, 9, 10]

list(map(lambda x , y : x + y , l1 , l2 ))

o/p: [7, 9, 11, 13, 15]

here, 1st position has 1 + 6 = 7,

2nd has 2 + 7 = 9,

3rd has 3 + 8 = 11 and so on...

We can also call with external function as:

def add(x,y): return x+y

list(map(add, l1, l2))

o/p : [7, 9, 11, 13, 15]

We can use lambda func or external func both with mapper func.

Mapper func only gives output according to whichever function and iterables we have put with it.

Example: We want uppercase of string s using mapper func:

s = "karan"

list(map(lambda s: s.upper() , s))

o/p: ['K', 'A', 'R', 'A', 'N']

Reduce Function:

Reduce works as similar to mapper functions but we need to call it from the library.

from functools import reduce

here, functools is the package name and reduce is function;

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

suppose we have a list l as above and we want to get sum of all elements.

reduce function reduces the whole list and gives us single output.

reduce has same signature as mapper func : func , iterable

Code:

reduce(lambda x , y : x + y , l)

O/p: 15

Working:

x y o/p

1 2

3 3

6 4

10 5

15 15

Here, x & y have respective values 1 & 2, then next iteration of x is the sum of 1 & 2 i.e. 3, and y goes to next iteration that is 3, then sum is 3 + 3 = 6 is next iteration of x and y is passed to next iteration and so on, until last of y is added which is 5, and sum total is 15.

Here, it has taken only 2 arguments x and y.

What if we take 3 arguments?

Lets check.

Code:

reduce(lambda x , y , z: x + y + z , l)

Working:

x y z o/p

1 2 3

6 4 5

15 15

Here, 1 + 2 + 3 = 6 is the new value assigned to x and 4 & 5 are assigned to y & z.

Theoretically, output should be 15 but there is error showing in Kernel.

error:

TypeError Traceback (most recent call last) Cell In[21], line 1 ----> 1 reduce(lambda x , y , z: x + y + z , l) TypeError: <lambda>() missing 1 required positional argument: 'z'

This because the reduce has property that the inside function must not have more than 2 arguments.

If we give blank list then:

reduce (lambda x , y : x + y ,[])

o/p:

TypeError                                 Traceback (most recent call last)
Cell In[23], line 1
----> 1 reduce (lambda x , y : x + y ,[])

TypeError: reduce() of empty iterable with no initial value

On passing single number list:

Code: reduce (lambda x , y : x + y ,[1])

o/p:

1

This is exceptional case of reduce function as y remains unassigned, but still sum is calculated.

Now we do multiplication:

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

reduce(lambda x , y : x * y , l)

o/p:

120

To find max:

reduce(lambda x , y : x if x > y else y, l)

o/p:

5

Filter function:

If there is collection like list and we want to filter data inside according to condition, we use filter function. It has same signature ans reduce func.

Filter function to find even number:

Code: list(filter(lambda x : x%2 == 0 , l))

o/p:

[2, 4]

For odd number:

list(filter(lambda x : x%2 != 0 , l))

o/p:

[1, 3, 5]

For negative numbers:

l1 = [-1,3,4,5,-6,7,-2,8,-9,-10]

list(filter(lambda x : x<0 , l1))

[-1, -6, -2, -9, -10]

We take another list:

l2 = ['karan' , 'raj' , 'kumar' , 'skills' , 'bengluru' , 'vishakhapatnam' ]

We want all strings with length less than 6 , i.e. <6 ;

Code: list(filter(lambda x : len(x) < 6 ,l2))

O/p:

['karan', 'raj', 'kumar']

Did you find this article valuable?

Support Karan's blog by becoming a sponsor. Any amount is appreciated!