Welcome to Python anonymous function tutorial. In the previous tutorial we learned about Python Recursion.
Table of Contents
What are Python Anonymous/Lambda functions?
In Python, a lambda function or the more commonly known, anonymous function, is a function defined with no names.
The reason why anonymous functions are also called lambda functions is that they’re defined with the use of the lambda keyword.
We assign a lambda function to a variable which becomes the object of the function. The same object can be used to call the function by specifying the arguments as a regular function.
Essentially, the variable becomes the name of the anonymous function to be used within the program.
Using Anonymous Functions in Python
Now that we know the basics behind anonymous functions, let’s explore how to put these functions to practical use
Basic Syntax of the Anonymous/Lambda functions
lambda arguments: expression
If you look at the syntax above, you’ll notice that lambda functions allow multiple arguments but a single expression. You can specify the arguments in a comma separated fashion.
Writing your first lambda function
I figured the easiest way to explain lambda functions to you is with comparisons. If you already know how to define a normal function, you’ll have a much better understanding of this section.
Take a look at the below example where I’ve created a function to return the sum of two arguments.
def sum ( a, b ):
return a+b
print (sum(1, 2))
print (sum(3, 5))
Easy? How about we convert this to an anonymous/lambda function?
sum = lambda a,b: (a+b)
print (sum(1,2))
print (sum(3,5))
As you can see, we’ve reduced the number of lines in our code here. However, one thing to note is that lambda functions can serve well for simple operations.
But, soon as you enter more complex operations, you sacrifice readability of your code with these functions.
Examples of Anonymous Functions in Python
Now, we are going to see some common use of lambda. By using the map() and filter() functions along with an anonymous function, we’ll change the items within a Python list.
In this case, we’ll increment all the elements held within the list by the number specified when asked for user input. In the below example, I’ve used a lambda function along with the map() method.
#initial list
l = [1,2,3,4,5,6,7,8]
#print the initial list
print("The initial list is: ")
print(l)
#choose n
n = int(input('Enter the value of n: '))
#increment all the values of the list by using map with help of lambda
l = list(map(lambda x: x+n , l))
#print the changed list
print("The changed list is: ")
print(l)
The output will be

Similarly, if we want to store the common elements of two lists into a new list, we can use the filter() function. To check if an element is in List2, we used the lambda function. The example code is given below
#initialize list1
list1 = [1,2,3,4,5,6,7,8]
#print list1
print("List1:", end = ' ')
print(list1)
#intialize list2 for select numbers
list2 = [2,3,5,9,12,14]
#print list2
print("List2:", end = ' ')
print(list2)
'''
compare using lambda function if the value is in list2, if yes then filer the result and assign to list3
'''
list3 = list(filter(lambda x: x in list2 , list1))
#print the changed list
print"List3 (List1 intersect List2 ):", end=' ')
print(list3)
The output of the above program will be

Why use Anonymous functions instead of a regular function in Python?
While it’s easy to replace an anonymous/lambda function in Python with a regular function, we cannot discard them as a programming gimmick.
For one, Python’s anonymous functions are especially useful when you have a function that accepts another function as an argument.
In such cases, it’s much easier to pass a one-off lambda function after assigning it to a variable.
Another important factor is how lambda functions take up fewer resources than named functions. If you need to create a function usable within a specific scope and nowhere else within a program, it doesn’t make sense to create an individual function for the same.
Lambda functions can get the work done without adding to the resource use in this case.
What is an expression within a Python lambda function?
The main difference between a statement and an expression is with their closing behavior.
A statement exits with no return value. An expression will return at least one value on exit.
Think of statements like print() where the function exits soon after printing the specified string.
Expressions, on the other hand, are like addition, subtraction, etc. where the output is the computed value.
Conclusion
So, that’s all about Python Anonymous Function. Hope that, Python Anonymous Function is clear to you now. For any further query, feel free to use the comment box. Good Luck!