Lambda, Filter, Map

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

ANONYMOUS FUNCTIONS IN PYTHON

Lambda FUNCTIONS
Contents:

• Anonymous/Lambda Function
• Filter()
• Map()
Anonymous/Lambda Function

• In Python, an anonymous function is a function that is defined without a name.

• While normal functions are defined using the def keyword in Python,
anonymous functions are defined using the lambda keyword.

• Hence, anonymous functions are also called lambda functions.


A lambda function in python has the following syntax.

lambda arguments: expression

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.

# Program to show the use of lambda functions


double = lambda x: x * 2

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

is nearly the same as:

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

# using the normally defined function


print(cube(5))
# using the lamda function
print(lambda_cube(5))
Output:
125
125
Lambda functions can take any number of arguments:

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:

numbers_list = [2, 6, 8, 10, 11, 4, 12, 7, 13, 17, 0, 3, 21]

filtered_list = list(filter(lambda num: (num > 7), numbers_list))

print(filtered_list)

Output

[8, 10, 11, 12, 13, 17, 21]


In the above example, we have created a list named numbers_list with a list of
integers. We have created a lambda function to check for the integers that are
greater than 7. This lambda function has been passed to the filter() function as the
argument and the results from this filtering have been saved into a new list named
filtered_list.
The map() Function

The map() function is another built-in function that takes a function object and a list.
The syntax of map function is as follows:

map(object, iterable_1, iterable_2, ...)

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

# We double all numbers using map()


numbers = (1, 2, 3, 4)
result = map(addition, numbers)
print(list(result))
Output :

[2, 4, 6, 8]
We can also use lambda expressions with map to achieve above result.

# Double all numbers using map and lambda

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]

result = map(lambda x, y: x + y, numbers1, numbers2)


print(list(result))

Output :

[5, 7, 9]
Consider the following example:

numbers_list = [2, 6, 8, 10, 11, 4, 12, 7, 13, 17, 0, 3, 21]

mapped_list = list(map(lambda num: num % 2, numbers_list))

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

# Take a list of numbers.


my_list = [12, 65, 54, 39, 102, 339, 221, 50, 70, ]

# use anonymous function to filter and comparing if divisible or not


result = list(filter(lambda x: (x % 13 == 0), my_list))

# printing the result


print(result)
Output:
[65, 39, 221]
THANK YOU

You might also like