+ 9

What is lambda in python ???

15th May 2019, 12:59 PM
Abhishek Gupta
5 Respuestas
+ 13
A lambda statement is used to create new function objects. the lambda takes a parameter followed by a single expression only which becomes the body of the function and the value of this expression is returned by the new function. E.g. Lambda p = [{'x': 5, 'y': 3}, {'x': 4, 'y': 1}] p.sort(key=lambda i: i['y']) print(p) Output : [{'y': 1, 'x': 4}, {'y': 3, 'x': 5}] we want to do a custom sort, and for that we need to write a function but instead of writing a separate def block for a function that will get used in only this one place, we use a lambda expression to create a new function. so lambdas are use to reduce the time to make another function using def and use that. it easily works with filter map and other functions very easily. https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2460/?ref=app https://www.sololearn.com/Discuss/1036672/?ref=app https://www.sololearn.com/Discuss/1675905/?ref=app https://stackoverflow.com/questions/890128/why-are-python-lambdas-useful
15th May 2019, 1:05 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 3
https://code.sololearn.com/c8J8ASFl4z9B/?ref=app
16th May 2019, 12:02 PM
Ayush Sinha
Ayush Sinha - avatar
+ 2
lambda is a keyword used to create functions much like def. Often it is recommended to use def to define functions, but there are special cases where lambda definitions are useful.
15th May 2019, 4:18 PM
Seb TheS
Seb TheS - avatar
0
lambda is used to create anonymous functions, usually these are the one liner function which can be called from some other function.
16th May 2019, 8:41 AM
Amit Jain
Amit Jain - avatar