Menu Close

Python Lambda ( Anonymous ) Function

python lambda function

In this tutorial, we are going to learn all about the Python lambda function. the anonymous function is also called the python lambda function. Here we will see what is python lambda function, syntax, how to create and use the lambda function.

What is the lambda function in python?

Python lambda or anonymous function is a function that is defined without a name, While function defines in python with a def keyword but in the lambda function is define without a name.
Python lambda function takes any number of arguments but can have only one expression. Python lambda function is defined with the lambda keyword.

Why use Python lambda function?

Python lambda function perform better, when you use them inside another function as a anonymous function.

Syntax

The Syntax of anonymous function in python is:-

lambda arguments: expression

Example 1:

To get the square of a number with single argument, use lambda function.

result = lambda x : x * x
print(result(5))

In the above example, x represents is the argument or parameter and x * x represents the expression.

Example 2:

Output:- 25

Python lambda function can be used to find the addition of more than two arguments.

result = lambda x,  y,  z : x + y + z
print(result(5,10,5))

Output will be:- 20

Example 2:

Python lambda function can be used to find the addition of more than two arguments.

result = lambda x, y, z : x + y + z
print(result(5,10,5))

Output will be:- 20

lambda function inside another function

Python Provides the best facility to use lambda or anonymous function inside another function.

def myFunc(n):
	return lambda x, y : x + y * name
	
result =  myFunc(5)
print(result(10,10))

Lambda function with map() functon

You can use a lambda function in python with the map() function. map() function is used to apply a function on each item of specified iterable (list, set, tuple).
map() function takes two parameters one is a function and the second is iterable.

Example

a = [2, 3, 4, 5, 6, 7, 8, 9]

x = list(map(lambda y : y * 2, a))
print(x)

Output will be:- [4, 6, 8, 10, 12, 14, 16, 18]

Lambda function with filter() functon

Python anonymous function is also used with filter() function. filter() function is used to construct iterator from elements of iterable for which function return true. filter() function take two arguments function and iterable.

Example

We have a python list old_list that contains some numeric value, and we will lambda function and filter function to filter only those number, greater than 10.

old_list = [2, 3 , 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15]

new_list = list(filter(lambda x : (x >= 10), old_list))
print(new_list)

Output will be:- [10, 12, 13, 14, 15]

Conclusion

In this tutorial, you have learned all about the Python anonymous function along with multiple examples. In Python programming, the lambda function is mostly used by python programmers. Python lambda function takes multiple arguments or parameters in a single statement.

If this article help you, please visiting for further python interesting tutorials.

For more information:- Click Here

Python Function Tutorial
Python f-string Tutorial

Was this post helpful?

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *