In this article we will learn about the Python Anonymous function and uses of anonymous function in python. Python Lambda function is the anonymous function.
Python Anonymous/Lambda Function
A function which is defined without any name is known as the anonymous function. In python, Lambda function is known as the anonymous function. The anonymous function are called lambda function because they are declared with the use of lambda keyword. In anonymous function a very small piece of code is included. During the requirement of function objects, anonymous functions are very useful.
How to use Lambda function in python?
The Syntax of Python Lambda function is as follows;
lambda arguments: expression
The Lambda function can have as many arguments as possible but in return they can give only one expression.
Python lambda function Examples
illustration no.1
Here, we will add 15 to x argument and then it will return the result;
p = lambda x: x + 15
print(p(12))
Output
27
Illustration no.2
In this example, we will multiply the x argument with the y argument and then it will return the result;
p = lambda x, y : x * y
print(p(12, 13))
Output
156
Illustration no.3
Here in this example, we will sum the x, y and z argument and then it will return the result;
p = lambda x, y, z : x + y + z
print(p(12, 13, 14))
Output
39
Use of Anonymous/Lambda Function
The Lambda function is generally used along with the built-in function of python programming language such as , and function.
• Using Lambda Function with filter()
The python function takes the list and function as an argument. It offers an effective way to separate all the elements from a sequence. After the calling of function with the items of the list, it return a new list or sequence in which the function evaluates to True.
Python Lambda Function with filter() Example:
mylist = [14, 51, 42, 69, 2, 11, 23, 12]new_list = list(filter(lambda x: (x%2 == 0) , mylist))
print(new_list)
Output
[14, 42, 2, 12]
• Using Lambda Function with map()
The python function takes a list and function and it gives a new list which contains all the items that are lambda modified that the function returns for each items.
Python Lambda Function with map() Example:
mylist = [5, 10, 11, 16, 17, 21, 43, 52]new_list = list(map(lambda x: x * 2 , mylist))
print(new_list)
Output
[10, 20, 22, 32, 34, 42, 86, 104]
• Using Lambda Function with reduce()
The python function accepts a list and function as arguments. The reduce() function is called with the lambda function and then it will return an iterable new reduced result. The functools module includes the reduce() function.
Python Lambda Function with reduce() Example:
from functools import reducemylist = [15, 40, 70, 30, 52, 7]
add = reduce((lambda x, y: x + y), mylist)
print (add)
Output
214
Conclusion
Above we have learnt about the Python Anonymous function and uses of anonymous function in python. A function which is defined without any name is known as the anonymous function. The anonymous function are called lambda function because they are declared with the use of lambda keyword. The Lambda function is generally used along with the built-in function of python programming language such as filter() and map() function.