What are Lambda functions? Lambda function in Python



To loops are the antithesis of efficient programming. They are still required and are the first conditional loops taught to Python beginners But in my opinion, They leave a lot to be desired.

Are by loops can be cumbersome and can make our Python code bulky and messy. But wait, What is the workaround? Lambda functions in Python!

Lambda functions give a data scientist a double boost. You can write neater Python code and speed up your machine learning tasks. The trick lies in mastering lambda functions and this is where beginners can stumble.


Initially, I also found the lambda functions difficult to understand. They are short-lived, but they may seem confusing like newcomers. But once I understood how to use them in Piton, I found them very easy and powerful. And I'm sure you will too by the end of this tutorial.

Then, in this article, you will learn about the power of lambda functions in Python and how to use them. Let's start!

Note: New to Python? I highly recommend checking out the free courses below to get up to speed:

What are Lambda functions?

A lambda function is a small function that contains a single expression. Lambda functions can also act as anonymous functions where they do not require any names. They are very useful when we have to perform small tasks with less code.

We can also use lambda functions when we have to pass a small function to another function. Do not worry, We will cover this in detail soon when we see how to use lambda functions in Python.

Lambda functions were first introduced by Alonzo Church in the 1920s. 1930. Mr. Church is well known for the lambda calculus and the Church-Turing Thesis.

Lambda functions are useful and used in many programming languages, but here we will focus on using them in Python. In Python, lambda functions have the following syntax:


Lambda functions consist of three parts:

  • Keyword
  • Variable / bound argument, Y
  • Body or expression

The keyword is required and must be a lambda, while the arguments and body can change as per requirements. You must be wondering why you should go for lambda functions when you have other regular functions. Fair question, let me expand on this.

Lamba function comparison with regular functions


Lambda functions are defined by the keyword lambda. They can have any number of arguments, but only an expression. A lambda function cannot contain statements and returns a function object that can be assigned to any variable. They are generally used for one-line expressions.

Regular functions are created using the def keyword. They can have any number of arguments and any number of expressions. They can contain any declaration and are generally used for large blocks of code.

IIFE using lambda functions

IIFEs are immediately invoked function expressions. These are functions that run as soon as they are created. IIFE do not require an explicit call to invoke the function. In Python, IIFE's can be created using lambda function.

Here, I have created an IIFE that returns the cube of a number:

(lambda x: x*x*x)(10)



Lambda function application with different functions

Time to jump into Python! Power up your Jupyter Notebook and let's get down to business.

Here, I have created a random data set containing information about a family of 5 people with their identification, Names, ages and income per month. I will use this data frame to show you how to apply lambda functions using different functions in a data frame in Python.



Lambda with Apply

Let's say we have an error in the variable age. We register ages with a difference of 3 years. Then, to remove this bug from pandas data frame, we have to add three years to the age of each person. We can do this with the request() run on Pandas.

request() The function calls the lambda function and applies it to each row or column in the data frame and returns a modified copy of the data frame:

df['age']=df.apply(lambda x: x['age']+3,axis=1)


We can use the request() function to apply lambda function to rows and columns of a data frame. If he axis argument in the request() function es 0, so the lambda function is applied to each column, and it is 1, the function is applied to each row.

request() The function can also be applied directly to a Pandas series:

df['age']=df['age'].apply(lambda x: x+3)


Here, you can see we got the same results using different methods.

Lambda with filter

Now, Let's see how many of these people have more than 18 years. We can do this using the filter() function. the filter() The function takes a lambda function and a Pandas series and applies the lambda function on the series and filters the data.

This returns a sequence of Certain Y Fake, what we use to filter the data. Therefore, the input size of the Map() The function is always greater than the output size.

list(filter(lambda x: x>18,df['age']))


Lambda with map

May be related to the following statement. 🙂 It is performance evaluation time and the income of all employees increases by 20%. This means that we have to increase each person's salary by 20% in our Pandas data frame.

We can do this using the Map() function. This Map() the function maps the series according to the input correspondence. It is very useful when we have to replace a series with other values. In Map() functions, the size of the inlet equals the size of the outlet.

df['income']=list(map(lambda x: int(x+x*0.2),df['income']))


Lambda with Reduce

Now, let's see the total income of the family. To calculate this, we can use the reduce() function in python. It is used to apply a particular function to the list of elements in the sequence. the reduce() The function is defined in the ‘functools’ module.

To use the reduce() function, we have to import the functools module first:

import functools
functools.reduce(lambda a,b: a+b,df['income'])



reduce() The function applies the lambda function to the first two elements of the string and returns the result. Later, stores that result and again applies the same lambda function to the result and the next element in the series. Therefore, reduce the series to a single value.

Note: Lambda functions in reduce() cannot take more than two arguments.

Conditional Declarations Using Lambda Functions

Lambda functions also support conditional declarations, como if..else. This makes lambda functions very powerful.

Let's say that in the family data frame we have to categorize people into ‘Adults’ or 'Children'. For this, we can simply apply the lambda function to our data frame:

df['category']=df['age'].apply(lambda x: 'Adult' if x>=18 else 'Child')


Here, you can see that Ryan is the only child in this family and the rest are adults. That was not that difficult, truth?

Whats Next?

Lambda functions are quite useful when working with a lot of iterative code. They seem complex, as i understand, but I am sure you will have understood its importance in this tutorial.

Share this article and comment below in case you have any questions or comments. Here, I have listed some blogs and in-depth courses related to data science and Python:



Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.