lambda function in python

Lambda Functions in Python

Lambda functions in python are single expression functions that are not bound to a name. This is why lambda functions are also called anonymous functions. In this tutorial, we’ll look at lambda functions, how are they different from normal functions, and where are they used along with some examples.

lambda function in python
  • Lambda Function Syntax
  • How are lambda functions different from normal functions?
  • When do we use lambda functions?

Lambda functions in python are defined using the lambda keyword. They are single expression functions. We use the following syntax to define them:

lambda arguments : expressions

Example,

add_10 = lambda x: x+10
print(add_10(15))

The above code gives the following output on execution:

25

In the above example, the statement lambda x: x+10 returns a function object which is stored in add_10. This function object can be used like any normal function.

Lambda functions can have any number of arguments but only a single expression.

Example:

a = lambda x,y,z: x+y+z
print(a(1,2,3))

The above code gives the following output on execution:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

6

In the above example, the lambda function has three arguments, x, y, and z and a single expression x+y+z.

Lambda functions in python work in a similar way as regular functions do. But, there are some differences between the two:

  1. Lambda functions are defined using the lambda keyword and are not bound to a name. Regular functions, on the other hand, are defined using the def keyword and have a name associated with them.
  2. Lambda functions are restricted to a single expression while normal functions can have any number of expressions. Lambda functions have an implicit return statement. During execution, a lambda function evaluates its expression and automatically returns the result.

The example below illustrates the difference between the two:

# Function to add two numbers
def add(x,y):
    # return the sum
    return x+y
add(10,20)

Output:

30

add = lambda x,y: x+y
print(add(10,20))

Output:

30

In the above example, the same task of adding two numbers is performed by a regular python function and a lambda function.

You may be wondering that if lambda functions and regular functions do the same thing, then why do we use them in the first place?

  • If a function is required for a single use simple task, lambda functions can be a good alternative. However, one should avoid writing complicated lambda functions that hamper code readability only to save a couple of lines of code.
  • Lambda functions can be used to return a function from another function. See the example below:
# A function returning another function
def add_n(n):
    # returns a lambda function to add n
    return lambda x: x+n

# Function to add 10 to a number
add_10 = add_n(10)

# calling the function
print(add_10(5))

Output:

15

In the above example, the function add_n(n) takes in an argument n and returns a lambda function to add that particular argument.

Lambda functions can be used for other use cases as well. For more, check out this answer on stack overflow.

If you’d like to know about regular functions in python check out our article on python functions.


In this tutorial, we looked at lambda functions in python. If you found this article useful do give it a share! For more such articles subscribe to us.

If you’re a beginner looking to start your data science journey and learn python, check out our Python for Data Science Series.

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top