what is a lambda function in python ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what is a lambda function in python ?

19th Jun 2023, 6:27 AM
Stephen Anim
Stephen Anim - avatar
3 Answers
+ 2
Hi, Stephen Anim ! The lambda function is a function, but instead of using def when you create it, you will instead use the lambda keyword. One advantage over the more common function you create with def, is that it’s an expression instead of a statement. So you can create it in every place you can create/have an expression. But there are also some limitations, like that you can’t create a lambda function with multiple lines. So use it to create more simple functions on special places in your code.
19th Jun 2023, 6:43 AM
Per Bratthammar
Per Bratthammar - avatar
+ 2
Per Bratthammar this person's explanation is much clearer than mine, but here is my explanation, and my opinion: Lambda function is a small/annoymous function in Python that allows you to create a small(and simple) function, where you can use it to make a condition that matches the expression(Meaning you can define a small function that does some small thing, and can not have a long/multiple line as a regular function). Why is lambda useful? It helps to reduce code line into more clear one, if you don't want it to be longer.. lambda functions are often used with filter() and map()(Filter takes out the result that matches the condition, while map will apply the expression to all elements). Lambda syntax: lambda var_name: condition, (argument). Besides lambda can be useful for smaller functions, it's not a good practice to call one lambda function multiple times. You might want to define one function then use it any time you want.
19th Jun 2023, 7:57 AM
Dragon RB
Dragon RB - avatar