Lambda Function in Python

Lambda function is a function which does not have a name. You can also call it as an Anonymous function.

Till now you have created named functions in python with the help of def keyword. These functions are called by their name whenever required in the program. But with Lambda function, you can immediately create a function at the point where it is needed.

How to create a lambda function in Python?

Syntax:
lambda arguments : expression

The syntax of the lambda function is made up of four parts:

  1. Keyword lambda
  2. arguments: This function can accept more than one argument separated by a comma (,).
  3. colon: A semicolon is inserted between the list of arguments and the expression.
  4. expression: This expression may contain operations (arithmetic, logical etc).

A lambda function may have multiple arguments, but there should be a single expression only to which all the arguments must belong.

Example

lambda a1,a2 : a1+a2

Here, a1,a2 are arguments & a1+a2 is the expression.

How does Lambda function work?

The working of the lambda function is very simple and easy to understand.

  • The arguments present in the lambda functions are the values which you provide.
  • The operations present in the expression are performed on these values(arguments).
  • The result produced by this operation is returned to the caller of the function.
  • You know that Python treats every entity as an object, this implies that a lambda function is also an object. Therefore, the lambda function is always assigned to a variable.
    i.e 
    sum = lambda a1, a2 : a1+a2​

Now let create a simple program to understand the working of the lambda function.

This program will add the two numbers.

sum = lambda a1, a2 : a1 + a2 #creating the lambda function with 2 arguments.
addition = sum(10,20) #calling the function.
print(addition)

Output:

30

Explanation:

  • In this program, a lambda function is created with; arguments a1 and a2 & expression is 'a1 +a2'.
  • The lambda function is assigned to the variable sum. Now, sum acts as a function which takes two parameters ie a1, a2.
  • In the next statement, the function sum() is called with actual parameters as (10,20).
  • The return value of this function is assigned to a variable addition. Finally, the value of the variable addition is printed as output.

filter(), map(), reduce() function with lambda function in Python

Lambda function is mostly used with built-in High order function. Python allows you to pass a function as an argument to another function because functions are also seen as objects. Such functions are termed as High order Functions. filter() , map() and reduce() functions are high order functions in python.

1. filter(): This function is basically used to filter the data based on the parameters provided to it.

It takes two arguments.

  • A function
  • A sequence(list, string, tuple etc)

It evaluates a condition for every element of the sequence and returns a new sequence with only those elements for which the condition is True.

Syntax:
filter(function, sequence)

2. map(): This function performs an operation over each element of the sequence. The result of the operation on each element is returned in the form of a sequence.

Two arguments are passed to this function.

  • A function
  • A sequence(tuple, string, list etc)

Syntax:
map(function, sequence)

You can also typecast the return value of filter() and map() functions.
Example:

list(filter(function, sequence))
or
list(map(function, sequence))

The return value will be a list.

3. reduce(): This built-in function belongs to a module called functools. So to use this function, it is imported from the mentioned module.
It also takes two arguments:

  • A function
  • A sequence.

Syntax:
reduce(function, sequence)

 Working of reduce() function:

  • reduce() function performs the specified operation over the first 2 elements of the sequence.
  • In the next step, the obtained result becomes the first element, thus the operation is performed over the previous result and the next element.
  • In this way, the operations are performed until the sequence is exhausted.
  • The final result is returned to the caller of the function.

Let us now create a program using the above functions with lambda function. 

from functools import reduce #importing the built-in function reduce() from module functools

L1 = [45,33,76,90,25,11,10,20]
print('L1 =', L1)

print('\nPick out all the multiples of 5 from list L1.')
multi_five = list(filter(lambda m : m%5==0, L1))
print(multi_five)

print('\nDivide all the above multiples by 5.')
div = list(map(lambda d : d/5, multi_five))
print(div)

print('\nAdd all the above numbers.')
sum = reduce(lambda s1,s2 : s1+s2, div)
print(sum)

 Output:

L1 = [45, 33, 76, 90, 25, 11, 10, 20]

Pick out all the multiples of 5 from list L1.
[45, 90, 25, 10, 20]

Divide all the above multiples by 5.
[9.0, 18.0, 5.0, 2.0, 4.0]

Add all the above numbers.
38.0


A lambda function proves to be very useful when you want to make use of a function for a very less duration. In case your function contains only one operation say addition. Then it seems pointless to write 3-4 line function for a single operation. Lambda functions are used in such a situation which make your program more efficient.