Python Practice Map Filter Reduce

In this class, We do Python Practice Map Filter Reduce.

For Complete YouTube Video: Click Here

The reader will have a more profound understanding of the concepts of python with these examples.

Take the complete placement training course and solve all the practice tests.

The reader can easily crack the campus placement exams with our placement training course.

Q1)
What is the output displayed by the below code?

list1=["abcd","efg"]
out=list(map(list,list1))
print(out)

In the above program, we are using the map function.

For a detailed explanation of the map, reduce, and filter function, Click here.

The input is a list of strings. The map function is applying the list function on the input list.

If we convert a string to a list, each character is considered an element to the list.

Below is the output displayed by the program.

Output:
[['a', 'b', 'c', 'd'], ['e', 'f', 'g']]

Q2)

What is the output displayed by below program?

string1="learning monkey"
string2="living monkey"
x=list(string1)
y=list(string2)
out=set(filter(lambda z: z in x,y))
print(out)

In the above program, we used the filter function.

The filter function will take a function and iterator as inputs.

In our example, the variable y is an iterator, and the characters in variable y are given as input to the lambda function.

Suppose the lambda function returns true. The filter function will keep the character. 

To eliminate duplicates. The output of the filter function is sent to the set data type.

Below is the output displayed by the program.

Output:

{'o', 'y', 'e', 'n', ' ', 'g', 'm', 'l', 'i', 'k'}

Q3)

What is the output displayed by the below code?

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

In the above program, we are using reduce function.

The reduce function will take the input from the iterator and give it one of the function inputs.

The other input to the function is the previous output from the function.

Initially, we have zero as input. Multiply by zero gives zero.

The output of the program is zero.