In this post, we will discuss the concept of Python Anonymous Functions or Lambda Functions. This chapter is also a continuation of our previous chapters based on the Python Functions. We’d suggest you go through the chapters of Python Functions to understand the concepts in this post.
Link to all the chapters of our Python Tutorial Series: Learn Python
Python Anonymous Functions or Lambda Functions
If a function does not have a name, it is called ‘Anonymous Function’. In our previous chapters, we came across the examples that were created using the def keyword. Well, the anonymous functions are not created that way and are created using the lambda keyword. That’s why the Anonymous Functions are also called Lambda Functions in Python.
For example, as per the concepts discussed in our previous chapters, if you were to write down a function to find the cube value of a number, you would declare it this way.
def cube(n):
return n*n*n
But the same can be rewritten as below using the lambda function
f=lambda n: n*n*n
Since the lambda is still a function and can potentially return values, it needs to be assigned to a function as mentioned above. In the format mentioned above, ‘f’ is the name of the function.
LAMBDA FUNCTION PROGRAM TO FIND THE CUBE VALUE OF A NUMBER
#lambda function to find the cube value of a number
f=lambda n:n*n*n #lambda function
result=f(3) #calling the lambda function
print("Cube value of 3 is: ",result)
Output
Cube value of 3 is: 27
Since the lambda functions contain only one expression and return the result anyway, we don’t necessarily have to use the ‘return’ statement.
LAMBDA FUNCTION PROGRAM TO SUBTRACT NUMBERS:
#lamba function program to subtract numbers
f=lambda x,y: x-y #lambda function
value=f(45,25)
print("The subtraction result is: ",value)
Output
The subtraction result is: 20
LAMBDA FUNCTION PROGRAM TO FIND THE SMALLEST BETWEEN TWO NUMBERS
#lamba function program to find the smallest
#between two numbers
min=lambda a,b: a if a<b else b #create lambda function
x,y=[int(i) for i in input("Enter numbers: ").split(',')]
print("The smallest number= ",min(x,y)) #calling the lambda function
Output
Enter numbers: 5,7
The smallest number= 5
LAMBDAS WITH FILTER() FUNCTION
The elements from a sequence can be easily filtered out using the filter() function based on the result of the function. The sequence and the function can be supplied with the filter function as mentioned below.
filter(function, sequence)
In the format mentioned above, the ‘function’ refers to the function name, and it may return either False or True. The sequence refers to a tuple, string, or list.
The function works on every element in the sequence using the filter function. Let us now look at a program to find the even numbers from a sequence to understand the same.
#program using #filter function to find the even numbers
def is_even_or_not(n):
if n%2==0:
return True
else:
return False
#declare a list
list1=[1,2,3,4,5,6,7,8,9,10]
#using the filter function now
even_list=list(filter(is_even_or_not,list1))
print(even_list)
Output
[2, 4, 6, 8, 10]
By this time, you already know that the lambda functions can reduce the code of a Python program by rewriting the defined functions with shorter code. Let us rewrite the above program with the filter function again, but with the lambda function instead of a regular function.
list1=[1,2,3,4,5,6,7,8,9,10]
#using the filter function now
even_list=list(filter(lambda n: (n%2==0),list1))
print(even_list)
Output
[2, 4, 6, 8, 10]
LAMBDAS WITH MAP() FUNCTION
The map() function is also like the filter() function, but it works on every element of the sequence and can maybe change the elements. Please refer to the format of the map() function below.
map(function, sequence)
In the map function, a particular operation is performed on the elements of a sequence using the function, and the updated elements are returned that can be stored in a different sequence.
#map() function to find the cubes of numbers
def cubes(n):
return n*n*n
#list of numbers
list1=[2,3,4,5]
#call map() with cubes() and list1
cube_list=list(map(cubes,list1))
print(cube_list)
Output
[8, 27, 64, 125]
The same program can be rewritten with a shorter code using the lambda function as mentioned below.
#map() function to find the cubes of numbers
list1=[2,3,4,5]
#call map() with cubes() and list1
cube_list=list(map(lambda n:n*n*n,list1))
print(cube_list)
Output
[8, 27, 64, 125]
LAMBDAS WITH REDUCE() FUNCTION
Just as the name sounds, the reduce() function in Python decreases a sequence of elements to a single value by processing the elements according to their function. Please refer to the format of the reduce() function here below.
reduce(function, sequence)
Let us now look at a program to understand how the reduce function works.
#Lambda that returns products of elements of a list
from functools import *
list1=[1,2,3,4,5]
outcome=reduce(lambda p,q: p*q, list1)
print(outcome)
Output
120
- We imported all functions from the functools module in the program above because the reduce() function belongs to this module.
- We declared a list, list1 with numbers 1 to 5 in it
- We added a reduce function in the following line and offered list1 as the sequence. Instead of providing a function, we declared a lambda function within the reduce function that will multiply the supplied two arguments.
- The first two elements, 1 and 2, are multiplied, and its result 2 is multiplied with the following number 3 ((1*2)*3). And its result 6((1*2)*3) is multiplied with the following number 4. And its result 24 (((1*2)*3)*4) gets multiplied with 5, and the final result 120 is then stored to the variable outcome.