Page Contents
Map function in Python loops over each item in one or more given iterables and returns a map-object (which is an iterator) by applying the given function to them. The number of arguments in the function given in the map function and the number of iterable in the map function must always be equal.
Syntax of map() function
map(func_name , iterable_name1 [, iterable_name2 , iterable_name3…….])
Parameters of map() function
- func_name =
map
function will apply this function to each item in the given iterable. - iterable_name =
map
will apply the given function to each item of this iterable.
Return type of map() function
It returns a map-object
.
Why to use Map function ?
Suppose, each item in a given list
is a number but their type is str
, Now if we want to change the type of all the item from str
to int
, our standard program will be –
list_of_no=["1","3","8","2","10"]
#to change all the item of the list into int
for i in range(len(list_of_no)):
list_of_no[i]=int(list_of_no[i])
print(list_of_no)
[1, 3, 8, 2, 10]
In the above code , each item of the list
is looped over and typecasted into int
.Then the list
is printed.
Now to solve the above problem with the help of map
function program will be –
list_of_no=["1","3","8","2","10"]
#to change all the item of the list into int
print(map(int,list_of_no))
print(list(map(int,list_of_no)))
<map object at 0x0000023E38839130> [1, 3, 8, 2, 10]
In the above code, list_of_no
is an iterable on which map
function has applied .map
function loop over the list_of_no
and apply int
function to each item. Since map
function returns a map-object
, we need to typecast the map-object
in the list
to return the list
.
So we can easily see that the code we have written in four lines with the help of for-loop
, same code we have written in just two lines with the help of map
function. Also since map
function is written in c, map
is more efficient than for-loop
. Also map
function consumes less memory.
How to take multiple integer input in a single line ?
Many times in programming we encounter problems where we have to take more than one integer input in the same line and we can do it very easily with the help of map
function.
print(list(map(int,input().split())))
[1, 5, 8, 3]
When we give 1 5 8 3
as input to the above code, the program will accept it as “1 5 8 3”
. Because input
function always takes a string as input.
We will then convert the input to a list
using the split
method and it will be [“1”, “5”, “8”, “3”]
. The list
is an iterable and since the type of each item in the list is str
, we applied the map
function to typecast the each item in the list
into int
. Then we typecast the map-object
into a list
, which returns [1, 5, 8, 3]
.
Map function with lambda expression
Suppose, each item in a given list
has to be replaced with their square. We will solve this problem with the help of map
function and lambda
expression.
list_of_no=[2,7,4,9,11,6]
print(list(map(lambda x:x**2,list_of_no)))
[4, 49, 16, 81, 121, 36]
Since list_of_no
is an iterable in the above code, when map
function is applied to list_of_no
, map
function applies lambda
expression to each item in list_of_no
and replaces item with output of lambda
expression.
That is, first the lambda
expression was applied to 2
of list_of_no
which made 2
into 2 ** 2 = 4
and replaced 2
with 4
. Then lambda
expression is applied to 7
of list_of_no
which makes 7
into 7 ** 7 = 49
and 7
is replaced by 49
. And the process continues in the same way until the list
is finished.
Map function with multiple iterable
Suppose we have two lists of numbers,list1
and list2
. We need to create a new list, list3
where list3 [0] = list1 [0] + list2 [0]
, list3 [1] = list1 [1] + list2 [1]
etc. Now we will solve this problem with the help of map
function.
list1=[1,5,3,8]
list2=[3,2,7,9]
list3=list(map(lambda x,y:(x+y),list1,list2))
print(list3)
[4, 7, 10, 17]
In the above code, lambda
expression will take two numbers x
and y
and return their sum. Now at first map
function will apply the lambda
expression to the 0th item of list1
and list2
which will return 1 + 3 = 4
. Then map
function will apply the lambda
expression to the 1st item of list1
and list2
which will return 5 + 2 = 7
. And it will continue like this until the list
ends.
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .