Anonymous Functions/ Lambda Function

Anonymous Functions/ Lambda Function

In Python anonymous functions are also known as lambda functions. In this section we will learn anonymous functions, how they are defined and declared, their syntax and examples.

An anonymous function has no name when it is defined in Python. The anonymous functions in Python are defined by the keyword lambda and not by the keyword def which was used to define normal functions. This is because anonymous functions are called lambda function.


Lambda Functions

The following is the syntax of lambda function:


lambda arguments: expression

The lambda functions can have a number of arguments but the expression in one only.


EXAMPLES of lambda functions

Consider the following example, in which a lambda function is used to increment a value by 1:


>>> increment = lambda a: a+1

>>> print(increment(2))




In the above example a lambda function is defined. The function simply takes an input and adds 1 to it, the number is then returned to the print statement and is printed. In the above example a is the argument that was passed and a+1 is the expression that is to be evaluated and returned. This function has no name and the returned object is stored in the identifier increment. The lambda function is called like a normal function.

The definition of the function that is:

increment = lambda a: a + 1

is similar to the definition of a simple normal function:

def increment (a):

           return a + 1


Use of Lambda Function

A lambda function is used when a temporary function is required for a short time period. Lambda functions are mostly used with built in functions and with higher ordered functions.


EXAMPLE use with filter ()

In Python the filter function takes in an argument with a list of items. The function is called with list of items and filter () function filters the iterator for which function has returned True. The new list is returned that contains items that are filtered and evaluated true.

Consider the following example in which filter () function is used to filter the list of items; the function filters the odd numbers from the list and returns the list:


>>> myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> myNewList = list(filter(lambda a: (a%2!=0), myList))

>>> print(myNewList)


[1, 3, 5, 7, 9]


In the above example, a list was declared that had integers from 1 to 9. Then we defined the lambda function and stored the returning object into myNewList identifier. In the second line of above lines of code the filter () function filters the list using the lambda function.

The lambda function has an argument a and the expression is (a%2! = 0). This function checks the items in list that if they are odd or not by dividing the items of the list by 2, if the numbers cannot be divided by 2 or the remainder is not equal to 0 then the numbers are odd numbers and odd numbers are returned in the form of a new list and new list is printed.


EXAMPLE use with map ()

The map () function is used to return an iterator which applies function to every item of iterable. This function is called with a list of items and returns a new list that has items on which the function was being performed.

Consider the following example, in which a list is declared and the function increments each item in the list by 1. The new list is then printed:


>>> myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

>>> myNewList = list(map(lambda a: a +1, myList))

>>> print(myNewList)


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


It can be seen in the above example that function increments each item of the list by 1 and new list is printed.