In this article we will discuss what is a lambda function in python and why they are required. Basically what are the use cases which makes use of lambda function must.

What is a lambda function ?

Lambda functions are also called Anonymous functions because they don’t have any name. Also they can be assigned to variables and passed to other functions as arguments.

Unlike other functions they are defined using ‘lambda’ keyword and syntax for their definition is as follows,

It accepts the arguments in args and returns the value evaluated by expression.

Let’s understand by an example,

Suppose we have a function that calculates the cube of a given number i.e.

Now, suppose we need to call this function only one time in our code. So, instead of creating a separate function we can create a lambda function that does the same work i.e.

While defining a lambda function we can assign this to a variable and call it using that variable i.e.

Output:

But wait, why would any one will create a lambda function ? As we said above the for one time tasks we can create lambda functions.
But if it’s one time task only, then we can write the code directly instead of creating any other function or lambda function like this,

Then why we need lambda functions ? Let’s look deeper to understand the need,

Need of a lambda function

An important aspect of a lambda function is that it can be passed to other function as arguments. This is the main thing that drives a need of
lambda function. Let’s understand this aspect by an example,

Suppose we have a function that accepts two arguments, a list of elements and callback function i.e.

This function iterates over all the elements in list and calls the passed callback() function on each of the element. Also, stores the value returned by each call to callback() function to an another list.

So, this function is basically converting values in list to some other values in new list. But what’s the conversion logic ? Conversion logic is passed to this function as a callback function.
Let’s use this function to convert a list of numbers to their cube i.e.

Output:

As we can see in output converter() returns a list of numbers which are basically cube of numbers passed in other list.

Important observation :

For calling converter() we need to pass a function as argument. Therefore we created a small cube() function. Now most probably this function will not be used by anywhere else. Also, if we are going to use converter() function again for some other conversions then we are again going to create some small functions, which will never be used again. So basically we are going to pollute our code.

Is there anyway to prevent this pollution of code with numerous one time used small functions ?
Yes, here comes the Lambda function in picture.

We can call the converter() function to convert the list of numbers to their cube by passing a lambda function in converter() as argument i.e.

Output:

So, basically instead of creating a separate function cube() we passed a lambda function to the converter() function.

Similarly we can call the converter() with another lambda function to convert the list of numbers to their square i.e.

Output:

Similarly we can call the converter() with another lambda function to convert the list of numbers from celsius to farhaneit i.e.

Output:

Now by using lambda function we prevented the creation of 3 small ONE TIME functions here. So, this is how lambda function prevents the creation of small one time functions and shows it’s usefulness.

Passing multiple arguments in lambda functions

We can also create lambda functions that accepts multiple arguments i.e.

Output:

Complete example is as follows,

Output:

 

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,