Lambda functions in Python

Lambda functions in Python anonymous function objects that are created by the lambda expressions through the keyword lambda. An anonymous function does not have a name. Lambda expressions are useful for writing small functions which are useful in narrow contexts.

Keywords Updated 8 Sep 2019 2 min read

Overview

The lambda keyword in Python is used for writing lambda expressions. These lambda expressions produce anonymous functions which are also called function objects.

These anonymous functions do not have a name. The function object returned from a lambda expression is called as lambda function.

A Python lambda expression is of the following form:
lambda p1, p2…pn:<valid Python expression>

where p1, p2...pn are the parameters to the lambda expression.
e.g., The lambda expression, area = lambda radius: 3.14*pow(radius, 2) produces a function object when invoked with the parameter radius - area(3), returns the area of a circle as 28.26 units.

In Python, statements and annotations are not allowed in a lambda expression. Though a lambda function does not have a name, they can be assigned to a variable and the variable name can be used to invoke the function object.

Except for the restriction of not allowing any statements or annotations and not having a name, the lambda function is very similar to a regular function.

Example 1 - Lambda expression in Python

This Python example uses a lambda function as a parameter to the built-in function filter to produce a new sequence. Based on the return value of the lambda function for each item in the original sequence, a new sequence is produced.

python
# Example Python program that prints only the numbers

            # that are multiples of five using a lambda expression

            # and the built-in function filter

            multiples_5 = filter(lambda num: num % 5 == 0,

                                 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

print("Multiples of five:")

            for value in multiples_5:

                print(value)

Output

Output
Multiples of five:

            5

            10

Example 2 - A Python function that returns a lambda expression

The lambda function produce() returns a lambda function for each value it receives for the parameter base. In mathematics, a function that returns another function is called a higher order function.

python
# Function that returns a lamda expression

            def produce(base):

                return lambda exp:pow(base, exp)

# Make squares

            squareFunction = produce(2)

            print("Squares of numbers:")

# Two square

            squareValue = squareFunction(2)

            print(squareValue)

# Three square

            squareValue = squareFunction(3)

            print(squareValue)

# Make cubes

            cubeFunction = produce(3)

            print("Cubes of numbers:")

# Two cube

            cubeValue = cubeFunction(2)

            print(cubeValue)

# Three cube

            cubeValue = cubeFunction(3)

            print(cubeValue)

Output

Output
Squares of numbers:

            4

            8

            Cubes of numbers:

            9

            27

Example 3 - A lambda expression using two parameters

python
# Example Python program that finds the

            # area of rectangles using lambda functions

            areaRect = lambda length, width: length*width

# Area of the rectangle1 whose length is 4 and

            # width is 3

            area1 = areaRect(4, 3)

            print("Area of the rectangle:{} units".format(area1))

# Area of the rectangle2 whose length is 6 and

            # width is 5

            area2 = areaRect(6, 5)

            print("Area of the rectangle:{} units".format(area2))

Output

Output
Area of the rectangle:12

            Area of the rectangle:30