Python – Lambda

Python Lambda

I would say lambda functions in Python were one of the things I had a hard time not only understanding but also seeing the practicality of using in much of my code. However once I used a lambda function a few times I found there are some instances where they are the best option in solving some problems. In this article I aim to explain what a lambda function is, its syntax, and an example using a lambda function.

 

What is a Lambda Function

A lambda function in python is an anonymous function, which is simply a function defined without a name. Defining a function normally starts with the def keyword followed by the function name then its arguments. With a lambda function you replace the def keyword with lambda and then immediately define the arguments it accepts which can be any amount. Now you may be asking why have this, the hint to this is that they are restricted to a single expression so knowing this, lambda functions are going to only be doing simple tasks. This will make more sense in the examples below.

 

Syntax

The syntax is very simple for lambda functions, they are simply 1 line and look similar to below.

As you can see we first call the lambda keyword, followed by our 2 arguments.

Next we have a colon which is what separates the arguments from the expression

Lastly our expression is simply adding our 2 arguments together

Notice that we don’t have any return statement, that is because we don’t need one. With lambda functions the return is implicit, whatever the output of the expression is will be returned out of the function automatically.

 

Basic Example

Lets take a look at the example code below and step through it.

We can see in line 1 we are assigning our lambda function to the variable ‘add’ then line 2 calls that lambda function with 1 and 3 as the arguments and prints out what it returns. Below is the output of running that code.

So we can see that it works but why would we choose to use a lambda function over just defining it like normal? Well read on and we will show a practical example of when a lambda function would be a good idea.

 

Practical Example

So in this example lets say that we have a function that accepts a list of dictionaries, and within that function it needs to extract a specific key from each dictionary and process it further. This would be a scenario where we could use a lambda function, in the first example I will show you an example without using a lambda function so we can establish a baseline.

Taking a look at this code the ‘age_extractor’ function is what we can turn into a lambda function, but stepping through the last line calls the handle_dictionaries which in turn calls the age_extractor function which returns us a list of ages, then the handle dictionaries function prints that out.

If we run this file about we get the following output.

Looking at the code above you can see we needed an entire extra function to handle getting the keys, which in turn leads to more jumping around when reading the code. Lets get the same functionality but use a lambda function.

You can see that now we completely got rid of the standard age_extractor function and turned it into a lambda function which was stored as a default argument on the handle_dictionaries method.

It is also important to understand that the lambda function, because it is only allowed a single expression on 1 line now uses a list comprehension vs a for loop. If you do not know what a list comprehension is you can read my article here.

Now if we run this updated code we get the following output.

As you can see we get the exact same output. Now I’ll be the first to admit that converting that function to a lambda function would not always be the correct decision, specifically if that function would need to be used by other functions or methods in our project but for the purposes of this demonstration we will assume that only that function needs that code.

 

In Conclusion

I hope this was able to clear up what a lambda function is, how it works, and provided you with an example where you can see how it can bring value to your code base. If this article was able to help you, please share on social media and if you would like to read up on some of my other python articles you can view some of my latest articles here.

Be the first to comment

Leave a Reply

Your email address will not be published.


*