Lambda, Filter, Map
Lambda, Filter, Map
Lambda FUNCTIONS
Contents:
• Anonymous/Lambda Function
• Filter()
• Map()
Anonymous/Lambda Function
• While normal functions are defined using the def keyword in Python,
anonymous functions are defined using the lambda keyword.
Lambda functions can have any number of arguments but only one expression. The
expression is evaluated and returned. Lambda functions can be used wherever function
objects are required.
Here is an example of lambda function that doubles the input value.
print(double(5))
Output
10
In the above program, lambda x: x * 2 is the lambda function. Here x is the argument and x * 2 is the
expression that gets evaluated and returned. This function has no name. It returns a function object
which is assigned to the identifier double.
The statement
double = lambda x: x * 2
def double(x):
return x * 2
# Python code to illustrate cube of a number
# showing difference between def() and lambda().
def cube(y):
return y*y*y
lambda_cube = lambda y: y*y*y
Example
Multiply argument a with argument b and return the result:
x = lambda a, b : a * b
print(x(5, 6))
Example
Summarize argument a, b, and c and return the result:
x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
Why Use Lambda Functions?
Lambda functions are used when you need a function for a short period of time. This
is commonly used when you want to pass a function as an argument to higher-order
functions, that is, functions that take other functions as their arguments.
The use of anonymous function inside another function is explained in the following
example:
def testfunc(num):
return lambda x : x * num
In the above example, we have a function that takes one argument, and the argument is
to be multiplied with a number that is unknown. Let us demonstrate how to use the above
function:
def testfunc(num):
return lambda x : x * num
result1 = testfunc(10)
print(result1(9))
Output
90
In the above script, we use a lambda function to multiply the number we pass by 10.
The same function can be used to multiply the number by 1000:
def testfunc(num):
return lambda x : x * num
result2 = testfunc(1000)
print(result2(9))
Output
9000
The filter() Function
The Python's filter() function takes a lambda function together with a list as the
arguments. It has the following syntax:
filter(object, iterable)
The object here should be a lambda function which returns a boolean value. The
object will be called for every item in the iterable to do the evaluation. The result is
either a True or a False for every item. Note that the function can only take one
iterable as the input.
A lambda function, along with the list to be evaluated, is passed to the filter() function.
The filter() function returns a list of those elements that return True when evaluated
by the lambda function. Consider the example given below:
print(filtered_list)
Output
The map() function is another built-in function that takes a function object and a list.
The syntax of map function is as follows:
The iterable to the map() function can be a dictionary, a list, etc. The map() function
basically maps every item in the input iterable to the corresponding item in the output
iterable, according to the logic defined by the lambda function.
# Python program to demonstrate working of map.
# Return double of n
def addition(n):
return n + n
[2, 4, 6, 8]
We can also use lambda expressions with map to achieve above result.
numbers = (1, 2, 3, 4)
result = map(lambda x: x + x, numbers)
print(list(result))
Output :
[2, 4, 6, 8]
# Add two lists using map and lambda
numbers1 = [1, 2, 3]
numbers2 = [4, 5, 6]
Output :
[5, 7, 9]
Consider the following example:
print(mapped_list)
Output
[0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1]
In the script above, we have a list numbers_list, which consists of random numbers.
We then call the map() function and pass it a lambda function as the argument.
The lambda function calculates the remainder after dividing each number by 2. The
result of the mapping is stored in a list named mapped_list.
Finally, we print out the contents of the list.
Example:
# Python Program to find numbers divisible by thirteen from a list using anonymous
function