Map() Function in Python

The map() function applies function to every item of the passed iterable, yields the results, and returns an iterator.

Parameters

function– It is a function in which a map passes each item of the iterable.

iterables– It is a sequence, collection or an iterator object which is to be mapped.

Example no. 1 (In this situation we are squaring the number like 2 X 2 = 4)

# map(fun, iterable) gives you a map object
def square(x):
return x*x

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

s=list(map(square,1))

print(s)

Output:-

[1, 4, 9, 16, 25, 36]

 

Example No. 2 (In this situation we are multiplying a number to the number times like number 3 is multiple 3 time, number 6 is multiple 6 times.)


# example with Lambda function
m=list(map(lambda x:x**x,[1,2,3,4,5,6]))

print(m)

Output:-

[1, 4, 27, 256, 3125, 46656]

 

Example no. 3 ( In this situation first time we are printing the array and second time we converting integer into string)


# example with inbuilt function

list1=[2,3,4,5,6,7]
print(list1)

p=list(map(str,list1))
print(p)

Output:-


[2, 3, 4, 5, 6, 7]
[ ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’ ]

 

Check : HOW TO CHECK AND ENABLE ENA ON LINUX AND WINDOWS SERVER ON AWS

 

Leave a Reply

Your email address will not be published. Required fields are marked *