What are Python Lambda Functions?
Python Lambda functions are functions that do not have any name. Hence they are also known as anonymous or nameless functions. The word ‘lambda’ is a keyword. The function that follows the ‘lambda’ keyword is anonymous.
Functions Vs Lambda functions:
- To define normal functions in function in Python, we use the ‘def’ keyword. We use ‘lambda’ keyword to define anonymous functions.
- The lambda function can have any number of arguments, but they can return only one value in the form of expression. The Python lambda functions are syntactically restricted to a single expression.
Why use Python Lambda Functions?
- Lambda functions come into the picture when we need a function only once.
- Python Lambda functions are often used with built-in functions like filter(), map(), reduce(), etc.,
- Using Lambda functions reduces the number of lines of code compared to normal functions.
Syntax:
lambda arguments:expression
As mentioned already, a lambda function can take any number of arguments but a single expression. Hence the number of arguments ranges from 0 to any number.
Examples:
With a single argument:
Here is an example to print the square root of a number.
a=lambda x:x**(1/2) print(a(225))
15.0
x – is the argument.
x**(1/2) – The expression that gets evaluated.
lambda x:x**(1/2) – anonymous function which returns a function object.
>>> print(lambda x: x**(1/2)) <function <lambda> at 0x048388A0>
With multiple arguments:
Below is an example of a python lambda function that accepts multiple parameters.
a=lambda a,b,c:a+b+c print(a(3,10,12))
25
When we write the same example using normal functions, which will take more lines. For instance,
def func(a,b,c): return a+b+c print(func(3,10,12))
You might get this question, when we say lambda as nameless functions, then why we need to assign lambda function to some variable and call the lambda function using a variable. In general, we use lambda functions with higher-order functions and built-in functions like filter, map, reduce, etc.,
Lambda functions with normal functions:
def func(x): return lambda :x*x a=func(4) b=func(5) print(a()) print(b())
16 25
In the above example, the function func() returns a lambda function. Whenever we call the func(), we make use of the lambda function.
lambda function with filter:
The filter function filters the iterable with the help of another function. The filter function takes the function to filter the iterable and iterable as an argument.
Syntax:
filter(function, iterable)
Example:
Let’s see an example to filter the even numbers from the list.
l=[7,4,9,2,3,8,5,12,10] new_l=list(filter(lambda a:a%2==0,l)) print(new_l)
[4, 2, 8, 12, 10]
lambda function with map:
The map function applies a function to all the values in an iterable. The map function takes the function to be applied to the iterable and iterable as an argument.
Syntax:
map(function, iterable)
Example:
Get the even numbers from a list using the map function.
l=[12,14,3,9,15,25] new_list = list(map(lambda x: (x%2 == 0), l)) print(new_list)
[True, True, False, False, False, False]
lambda function with reduce:
The reduce function applies a function to all the values of an iterable and returns a single number. The reduce function takes the function and iterable as an argument.
Syntax:
reduce(function, iterable)
Example:
Get the product of all the numbers in a list.
from functools import reduce l=[12,14,3,9,15,25] product=reduce((lambda x,y:x*y), l) print(product)
1701000