Skip to Main Content
It looks like you're using Internet Explorer 11 or older. This website works best with modern browsers such as the latest versions of Chrome, Firefox, Safari, and Edge. If you continue with this browser, you may see unexpected results.

Python for Basic Data Analysis: 2.8 Lambda Expressions

Get started on your learning journey towards data science using Python. Equip yourself with practical skills in Python programming for the purpose of basic data manipulation and analysis.

What are lambda expressions?

Lambda expressions does what a function does without the need to properly define the function using def.

The body of a lambda expression will be similar to what we will put into the return of a function defined with def.

Let's take a look at an example using a multiplication function.


Since this function only has return we can even write it in 1 line.


To simplify that even further, we can use the lambda expression.

lambda x: x*20

You may notice that if you simply run the lambda expression you will get 

<function __main__.<lambda>(x)>

This isn't very helpful. So in order to make use of a lambda expression, we assign it a variable.


Now you might be asking, why are we using this lambda expression instead of a normal function?

This is because there are times when you only need to use the function once so you do not have to define it formally.

For example, if we want to use the filter function.