In this tutorial, You can read about the anonymous function, also known as lambda
functions. You can understand what they are, how to use them, and their syntax (with examples).
We’ll be covering the following topics in this tutorial:
What is lambda function in Python?
A Lambda Function
in Python programming is an anonymous single-line function defined without a name. Although the def keyword in Python describes normal functions, the lambda
keyword defines anonymous functions. A lambda function may contain any number of arguments but can contain a single expression. These are beneficial if we have small tasks with fewer coding.
Syntax of Lambda Function in python
Syntax: lambda argument(s): expression
Lambda functions consist of three parts:
• Keyword
• Bound variable/argument, and
• Body or expression
The keyword is compulsory and must be a lambda, while the arguments and the body will vary depending on the circumstances.
How do you use lambda function in Python?
As described above, A Python lambda function may only provide a single expression with any number of arguments. The lambda operator has no statements and returns a function object to which we may add some variable.
For example:
# Program to show the use of lambda functions cube = lambda y: y*y*y print(cube(5))
Output:
125
In the above program, lambda y: y*y*y
is the lambda function. Here y
is the argument and y*y*y
is the expression that gets evaluated and returned.
There is no name for this function. It returns a function object allocated to the identifier cube. We may term it a normal function. The statement
cube = lambda y: y*y*y
is nearly the same as:
def cube(y): return y*y*y
Difference between a normal def defined function and lambda function.
• def
is a keyword not returning anything except in the local namespace generating a 'name.'
Lambda is a keyword that returns a function object and does not create a 'name'
in the local namespace.⠀
• A 'def'
function can contain any python code. An expression function defined with lambda must be evaluated, and thus statements like print
, import
, raise
cannot be included.
• The development of a lambda function is marginally faster than def
. The distinction is that a name entry is generated in the local table. The resulting function has the same speed of performance.
The following are the characteristics of Python lambda functions:
• There is any number of arguments for a lambda function
, but they contain just one expression. An expression is a piece of code that can return any value, and the lambda function implements that.
• The functions Lambda may use to return function objects.
• The lambda functions are syntactically restricted to only a single expression.
Use of Lambda Function in python
The Lambda functions in Python use along with built-in functions like filter()
, map()
, reduce()
, etc.
lambda() Function with filter()
The filter()
function in Python constructs an iterator from an iterable element for which a function returns true. The syntax of filter()
method is: filter(function, iterable)
.
# Python code to illustrate # filter() with lambda() list = [5, 7, 97, 77, 23, 73, 61] filter_list = list(filter(lambda x: (x>50) , list)) print(filter_list)
Output:
[97, 77, 73, 61]
lambda() Function with map()
In python, the map()
function is another built-in function that takes an function object and a list. The function is called using a lambda function, returns a list
, and a new list containing all the modified lambda items returned for each object. The map function syntax is as follows:
# Python code to illustrate # map() with lambda() # to get double of a list. cube = [1, 2, 3, 4, 5, 6, 7] cubed_list = list(map(lambda x: x*x*x, cube)) print(cubed_list)
Output:
[1, 8, 27, 64, 125, 216, 343]
lambda() Function with reduce()
A list is taken as an argument by the reduce()
function in Python. It behaves differently from map()
and filter()
. The reduce()
function is iterable and a new reduced result is returned. The procedure is replicated over the pairs of the iterable.
# Python code to illustrate # reduce() with lambda() from functools import reduce list = [1, 2, 3, 4, 5, 6, 7] multiply = reduce((lambda x, y: x * y), list) print (multiply)
Output:
5040