In this article, you’ll learn about the anonymous functions, which are also known as the Lambda functions. You’ll learn what they are, their syntax, and the way to use them with examples.
In python, an lambda function may be function that’s defined without a reputation. while defining normal functions, we are using the def keyword in python, but while defining anonymous functions we are using the lambda keyword. Hence, anonymous functions also are called Lambda functions.
we use lambda functions once we require a nameless function for a brief period of your time. In python, we generally use Lambda functions as an argument to a higher-order function (a function that takes in other functions as arguments).
How to use Lambda Functions in Python
Let’s learn this in more detail.
A lambda function in python has the subsequent syntax.
syntax of Lambda Function in Python:
lambda arguments : expression
The lambda functions can have any number of arguments but they have only one expression. Firstly, the expression is evaluated and then returned. we used Lambda functions wherever function objects are required.
Example of Lambda function in Python:
In this section, we will see the example of a lambda function that lambda x: x * 2 is the input value.
x: x * 2
In the above code, lambda x: x * 2 is the lambda function. Also, their x is the argument and x * 2 is the expression that gets evaluated and returned to the user.
This function has no name. It returns a function object which is assigned to the identifier double. we will now call it a standard function. The statement
double = lambda x: x * 2
is nearly identical as:
def double(x) : return x * 2
Use of Lambda Functions in Python
we use lambda functions once we require a nameless function for a brief period of your time.
In Python, we generally use Lambda functions as an argument to a higher – order function (a function that takes in other functions as arguments).
For Example, These are used together with built – in functions like filter(), map(), and reduce(), etc. which we will discuss later in this article.
why use Lambda Functions?
As you may see in the previous section, lambdas are treated identically to regular functions at the interpreter level. In a way, you will say that lambdas provide compact syntax for writing functions that return one expression.
However, you must know when it’s a decent idea to use lambdas and when to avoid them. During this section, you may learn a number of the look principles utilized by python developers when writing lambdas.
Lambda Functions in filter()
The filter function is employed to pick some particular elements from a sequence of elements.
The elements which can be selected are predicated on some pre-defined constraint. It takes 2 parameters:
- A function that defines the filtering constant.
- A sequence used in this function is an iterator such as lists, sets, tuples, etc.
For Example,
sequences = [10,2,8,7,5,4,3,11,0,1] filtered_answer = filter (lambda x: x > 6, sequences) print(list(filtered_answer))
output:
[10, 8, 7, 11]
code Explanation:
- Here, we declare a variable called filtered_answer, which can store the filtered values returned by the filter() function.
- A lambda function that runs on each element of the list and returns true if it’s greater than 6.
- Print the result returned by the filter function.
Lambda Functions in map()
The map function is to use a specific operation for each element in a sequence. it also takes 2 parameters:
- A function that defines how the operations to be performed on the elements.
- one or more sequences.
For Example,
In this example, we could make a program that prints the squares of numbers in a given list:
sequences = [10,3,5,7,9,2] squared_result = map (lambda x : x*x, sequences) print(list(squared_result))
Output:
[100, 9, 25, 49, 81, 4]
Code Explanation:
- we define a list contained having name sequences that contain some numbers.
- we declare a variable called squared_result which is able to store the mapped values.
- A lambda function runs on each element of the list and returns the square of that number.
- print the result returned by the map function.
Lambda functions in reduce()
The following steps are to be followed by the reduce() function for an output:
- perform the defined operation on the primary 2 elements of the sequence.
- save this result.
- Perform the operation with the saved result and therefore the next element within the sequence.
- repeat until no more elements are left.
it also takes two parameters:
- A function that defines how the operations to be performed
- A sequence used in this function is an iterator such as lists, tuples, etc.
For example,
from functools import reduce sequences = [1, 2, 3, 4, 5, 6] product = reduce (lambda x, y: x*y, sequences) print(product)
Output:
720
Code Explanation;
- Import reduce from the functools module.
- Here, we define a list container named sequences that contains some numbers.
- we declare a variable called product which can store the reduced value.
- A lambda function runs on each element step by step of the list and returns the product of that number as per the previous result.
- print the result returned by the reduce function.