Python Lambda Function(with examples)

Python lambda function

Hello Techies,

This tutorial is based on the Python Lambda function, In this blog, you will learn How to use the Python Lambda function with the help of multiple examples.

Below are the points we are covering in this blog:

  • Python Lambda Function(Introduction)
  • How to create create a Lambda Function
  • How to call Lambda Function
  • When to use lambda functions
  • Examples of lambda function in Python

Python Lambda Function(Introduction)

If the function has no name, such function will be known as anonymous function. This is also known as the lambda function.

Anonymous functions are defined using lambda keywords, not def keywords.

The lambda function is a short, anonymous function that takes up many arguments but only one expression. Lambda functions return an object assigned to a variable or use it as part of another function.

Features of python lambda function :

  • Python lambda function only contains expressions.
  • It cannot contain a statement in his body.
  • lambda’s syntax does not support type annotations.
  • It can be invoked immediately.
  • Lambda function returns a function.
  • In the Lambda function, there is no need to write a return statement.
  • You can use all the types of actual arguments.

How to create a Lambda Function

Lambda function does not need to be named during the function definition. We create it with lambda keywords instead of def keywords.

Syntax for Python Lambda Function

lambda argument_list : expression

Below is an explanation of the above syntax:

  • Python lambda function starts with lambda keyword.
  • Lambda can have several arguments separated by a comma function.
  • The expression always returns an object.

Examples of Python Lambda Function

lambda x: print(x)
lambda x, y: x + y  

How to call Lambda Function

The lambda function returns a function where you can store it in a function variable; You can call the Lambda function using this function variable. for example:

add = lambda x: x+1
add(10)

In the above example, lambda x: x+1 will return the function in variable add, and then we can pass the argument to add function variable.


When to use lambda functions

  • We use lambda functions when we need the anonymous function for a short period.
  • It is used only when you want to pass a function as an argument for high-order functions.
  • You can use Lambda functions with built-in functions like filter(), map(), and functools.reduce() etc.

Examples of lambda function in Python

Now we understand how the lambda function works in python, let’s try to understand the lambda function by giving some more examples and usage cases.

Addition program using Lambda Function

add = lambda x: x+1
add(10)

Output is:

11

Subtraction Program using lambda function

subtraction = lambda x, y: x - y
subtraction(10, 5)

Output is:

5

Map function with the lambda

myList2 = list(map(lambda x : x*2, range(5)))
print(myList2)

Output is:

[0, 2, 4, 6, 8]

Filter function with the lambda

myList2 = list(filter(lambda x : x%2 == 0, range(5)))
print(myList2)

Output is:

[0, 2, 4]

Reduce function with the lambda

from functools import reduce
sum = reduce(lambda x, y: x + y, [1,2,3,4,5])
print(sum)

Output is:

15

FAQ on Python lambda function

  1. What is lambda function in Python?

    The lambda function is a short, anonymous function that takes up many arguments but only one expression.

  2. How do you use lambda function in Python?

    Lambda function does not need to be named during the function definition. We create it with lambda keywords instead of def keywords.
    The syntax of the python lambda function is:
    lambda argument_list : expression

  3. Is lambda a list comprehension?

    List comprehension is used to create lists, lambdas are functions that process like other functions and thus return values or lists.


I hope you understand the concept of the Python lambda function. If you have any doubt please comment below.

Leave a Comment