lambda with map() function in python by R4R Team

lambda function-br> -In Python, anonymous function means that a function is without a name. As we already know that def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions.

Syntax-
lambda argument:expression

map() function-
-It is a python function which returns a list of the results after applying the given function to each item of a given iterable like list,tuple.

Syntax-

map(fun,iter)

Lambda with map function-
In map we require a function for argument, so at the place of the function, we create a lambda function.

Syntax-
map(lambda arg:exp,iter)


program-

#square the list by lambda
l=[1,2,3,4]
a=map(lambda x:x**2,l)
print("List is",l)
print("After sqaure of number, list is")
print(list(a))

#double the number
l=[4,3,2,1]
a=map(lambda x:x+x,l)
print("List is",l)
print("After double the number, list is")
print(list(a))


output-

List is [1, 2, 3, 4]
After sqaure of number, list is
[1, 4, 9, 16]
List is [4, 3, 2, 1]
After double the number, list is
[8, 6, 4, 2]


-In this program, we make two map() function with lambda, first one is for square the number, and second one for double the number in the list.
-Here item of list is pass as argument in the lambda because of the map() function and lambda return required value and all thing create a sequence and we print them.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!