The world’s leading publication for data science, AI, and ML professionals.

Python Lambda Function

In this article, you will learn more about lambda functions in python. How can we use lambda functions, what is the syntax of lambda…

In this article, you will learn more about lambda functions in python. How can we use lambda functions, what is the syntax of lambda functions and why do we need them?

Image Credits: TechGeekBuzz
Image Credits: TechGeekBuzz

What are lambda functions and what are its characteristics?

Lambda functions are also called anonymous functions. An anonymous function is a function defined without a name. As you know to define a normal function in Python, you need to use the def keyword. But in this case of anonymous functions, we use thelambda keyword to define the functions. Alonzo Church in the 1930s to the field of mathematics introduced the lambda functions.

Also, the whole code for this tutorial can be found on my GitHub Repository below:

Tanu-N-Prabhu/Python

The characteristics of lambda functions are:

  1. The lambda function can take many arguments but can return only one expression. Here the expression is nothing but the result returned by the lambda function.
  2. Lambda functions are syntactically restricted to return a single expression.
  3. You can use them as an anonymous function inside other functions.
  4. The lambda functions do not need a return statement, they always return a single expression.

Use lambda functions when an anonymous function is required for a short period of time. – I don’t why they say this, if you know, please let me know, I’m curious.


What is the syntax of lambda functions?

The syntax of lambda functions is shown below:

lambda arguments: expression

The expression is always executed and returned.

Lets us understand the working of lambda functions, we will just use simple logic to understand the working of lambda functions by adding two numbers. Firstly, I will write the program and then explain to you what it does?

add = lambda a: a + a
print(add(20))
40

Ok firstly did you understand anything from the program, don’t worry I will explain it to you step by step.

  1. First, define the anonymous/ lambda function by using lambda keyword.
  2. Just remember the syntax of lambda functions which is as shown below: lambda arguments: expression, then just use the variable "a" as an argument and "a + a" as the expression, remember we are adding two numbers (a + a).
  3. Then to store the final added result let us use a variable "add".
  4. And then just as calling a normal function, we shall call the lambda function with the name of its variable "add" by sending a parameter "20" to perform the addition and then print it "print(add(20))".
  5. This is the final step, here when we print the result we then get 40 because of "20+20" since we are adding "a+a". Look here I’m not using the return statement.

Now that we know how lambda function works its time to play with it. Lambda function takes multiple arguments, let us see if this statement is true or not.

add = lambda a, b, c: a + b + c
print(add(10, 10, 10))
30

Wow, the above statement is actually true, because I passed more than one argument to the lambda function and then it worked.


What is the difference between lambda functions and normal functions in python?

This is one of the hardest questions to answer, after doing some research I nearly came to answer the above question. I don’t want to make general assumptions rather I would like to answer this question with the help of an example:

Example 1: Adding three numbers using the normal function.

def add(a,b,c):
    return a+b+c
result = add(10, 10, 10)
print(result)
30
print(type(result))
int

Example 2: Adding three numbers using the lambda function.

add = lambda a, b, c: a + b + c
print(add(10, 10, 10))
30
print(type(add))
function

Based on the above two examples I have made a few differences between the normal function and the lambda function. I hope it is helpful.

You are wondering how did I calculate the execution time of the program. Its simple I just used the time library. Alright, I will provide you the code below.

import time
start_time = time.time()
add = lambda a, b, c: a + b + c
print(add(10, 10, 10))
print("--- %s seconds ---" % (time.time() - start_time))
30
 --- 0.0005075931549072266 seconds ---

Why should we use lambda functions?

We can use lambda functions as anonymous functions inside any normal functions of python. This is the actual superpower of lambda functions. Because of its simplicity, you can write a lambda function with no hassle. Now just think you need to write a normal function of adding every number and the current given number you have given.

Confused, OK imagine now you have the number 100 as constant and every time you need to add another number such as 200, 300, 400,…. to the existing number. Here, rather than defining another function inside the same function, you use the lambda function as shown below.

def add(n):
    return lambda a : a + n
result = add(100)
print(result(200))
300

You can get the job done in just one step of using a lambda function. Like I said the simplicity and the usability inside another function are the prime features of a lambda function compared to the normal functions.


Can we reuse the lambda function?

I guess the answer is yes because in the below example I can reuse the same lambda function to add the number to the existing numbers.

def add(n):
    return lambda a : a + n
result = add(100)
print(result(200))
print("-------------------")
print(result(300))
print("-------------------")
print(result(400))
print("-------------------")
print(result(500))
300 
------------------- 
400 
------------------- 
500 
------------------- 
600

Well, there you go you would have now got a better understanding of lambda functions. This is the end of the tutorial of "Python Lambda Functions", there are more important concepts such as maps, filters and many more. These are altogether different concepts, I shall write them as separate tutorials shortly. Stay tuned!!. Until then see you, have a nice day.


Related Articles