When is a lambda function advantageous over a normal function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

When is a lambda function advantageous over a normal function?

Ok so maybe my question seems ridiculous, however I cannot seem to find any real advantage to using a lambda function. In fact the only thing different I have encountered with lambda is that it makes a code less readable, which slows the debug process. Is there some obvious purpose for this or situation where this becomes essential?

20th Aug 2018, 3:07 PM
Anthany Martin
Anthany Martin - avatar
3 Answers
+ 2
I once did a challenge on codewars or some other ( not sure now) where I had a character limmit in defining a function. The lambda turned out to be shorter. def fun(x): return something x here vs fun=lambda x: something x here the bigest value is most probably using it inside something such as map or filter.
20th Aug 2018, 3:19 PM
Louis
Louis - avatar
+ 2
Anthany Martin Have look at the next handy function. It returns the index of the item in list x that is the closest to value n min(range(len(x)), key=lambda i: abs(x[i]-n))
20th Aug 2018, 3:34 PM
Louis
Louis - avatar
0
Louis , thank you for your answer! I have a follow up however, how is using lambda in map() for example different than a defined generator function? Or maybe I don't quite understand how map() works, that is entirely possible lol
20th Aug 2018, 3:25 PM
Anthany Martin
Anthany Martin - avatar