Lambda, also called the anonymous function, is a perfect way to handle mini function necessities.
It doesn’t require creating definition blocks or naming the function and it all happens in one line.
When you need a proper user defined function defining a proper user function with def
keyword works great.
But sometimes you just need to squeeze your function in another function, or apply it to a column in a data frame or to the elements of an iterable like a list.
The necessity for lambda
arises from those situations where you just need a one time quick solution in a practical way.
Although lambda
syntax can be slightly confusing in the beginning, it can be quite handy once you get a hang of it.
Advantages of lambda is generally it’s tidy, practical and compact and it gets the job done which is a defining a custom function and then using it.