In this article, we will try to learn about Python Lambda.
Definition
Lambda is a function defined without a name. This can take multiple arguments, but only one expression is allowed that is evaluated and returned. Where function objects are required, we can use the lambda function.
Syntax:
lambda arguments: expression
Example 1: The function below is used to calculate the cube of a number.
def cube(a):
return a*a*a
print(cube(3))
<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-01-e1612000776479.png" height="116" src="data:image/svg xml,” width=”239″>
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-02.png" data-lazy- height="83" src="data:image/svg xml,” width=”550″>
The above function can be written using lambda, as shown below:
p = lambda x : x*x*x
print(p(3))
<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-03-e1612001168306.png" height="64" src="data:image/svg xml,” width=”277″>
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-04.png" data-lazy- height="81" src="data:image/svg xml,” width=”550″>
Example 2: The function below is used to calculate the sum of two numbers.
def sum_2(x,y):
return x y
print(sum_2(10,20))
<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-05.png" height="95" src="data:image/svg xml,” width=”270″>
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-06.png" data-lazy- height="110" src="data:image/svg xml,” width=”545″>
The above function can be written using lambda, as shown below:
p = lambda x,y : x y
print(p(10,20))
<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-07-e1612021523117.png" height="72" src="data:image/svg xml,” width=”290″>
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-08.png" data-lazy- height="110" src="data:image/svg xml,” width=”545″>
Example 3: The example below for lambda takes multiple arguments.
p = lambda x, y, z : x y z
print(p(10, 5, 3))
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-09-e1612021867349.png" data-lazy- height="64" src="data:image/svg xml,” width=”398″>
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-10.png" data-lazy- height="114" src="data:image/svg xml,” width=”604″>
Example 4: This function multiplies the number by 2 and can be written using Lambda function as below:
def func(n):
return lambda x : x * n
multiply_by_2 = func(2)
print(multiply_by_2(11))
print(multiply_by_2(15))
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-11-e1612022150976.png" data-lazy- height="158" src="data:image/svg xml,” width=”347″>
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-12.png" data-lazy- height="112" src="data:image/svg xml,” width=”502″>
Example 5: The function takes function as an argument and returns the result.
function_argument = lambda p, f: p f(p)
print(function_argument(5, lambda p: p * p))
print(function_argument(10, lambda x: x – 3))
print(function_argument(10, lambda x: x 5))
print(function_argument(10, lambda x: x / 5))
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-13.png" data-lazy- height="131" src="data:image/svg xml,” width=”589″>
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-14.png" data-lazy- height="172" src="data:image/svg xml,” width=”564″>
Example 6: In the example below, lambda is used to sort the values.
#(name,surname,age)
data = [(“Sachin”, “Tendulkar”, “42”), (“Rahul”, “Dravid”, “44”), (“Virendra”, “Sehwag”, “40”)]
data.sort(key=lambda x:x[0])#sort based on name
print(data)
data = [(“Sachin”, “Tendulkar”, “42”), (“Rahul”, “Dravid”, “44”), (“Virendra”, “Sehwag”, “40”)]
data.sort(key=lambda x:x[1])#sort based on surname
print(data)
data = [(“Sachin”, “Tendulkar”, “42”), (“Rahul”, “Dravid”, “44”), (“Virendra”, “Sehwag”, “40”)]
data.sort(key=lambda x:x[2])#sort based on age
print(data)
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-15.png" data-lazy- height="187" src="data:image/svg xml,” width=”974″>
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-16.png" data-lazy- height="200" src="data:image/svg xml,” width=”974″>
Now, go into python3 interpreter.
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-17.png" data-lazy- height="139" src="data:image/svg xml,” width=”974″>
The lambda function is used in many inbuilt methods. The following are some examples:
1. Map
This function maps each element in sequence using the lambda function.
Syntax:
map(function, seq)
Ex:
nums = [1,2,3,4,5,6]
Here, we will multiply each element in the list by 2.
mul_2 = map(lambda x: x*2, nums)
print(list(mul_2)) # It returns map object and typecasting it as list.
In the above function, each element of thelist is passed to the lambda function and the lambda function will multiply it by 2.
nums = [1,2,3,4,5,6]
mul_2 = map(lambda x: x*2, nums)
print(list(mul_2))
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-18-e1612023157667.png" data-lazy- height="89" src="data:image/svg xml,” width=”399″>
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-19.png" data-lazy- height="122" src="data:image/svg xml,” width=”479″>
2. Filter
This function filter out all the elements of a list for which the lambda function returns True.
Syntax:
filter(function, seq)
Ex:
nums = [0,1,2,3,4,5,6,7,8,9,10]
odd_nums = filter(lambda x: x % 2, nums)
print(list(odd_nums)) # It returns map object, and typecasting it as list.
nums = [0,1,2,3,4,5,6,7,8,9,10]
odd_nums = filter(lambda x: x % 2, nums)
print(list(odd_nums))
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-20-e1612023677731.png" data-lazy- height="90" src="data:image/svg xml,” width=”505″>
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-21.png" data-lazy- height="110" src="data:image/svg xml,” width=”535″>
3. Reduce
This function returns a single value by applying function func() to the seq.
Syntax:
reduce(func, seq)
Ex:
nums = [0,1,2,3,4,5,6,7,8,9,10]
value = reduce(lambda x,y: x y, nums)
print(value)
In the above list, it will take the first 2 elements and perform addition. The result of an addition will be added to the third element and so on. Finally, it will return a single value.
Note: This method is not available in the python3 version.
nums = [0,1,2,3,4,5,6,7,8,9,10]
value = reduce(lambda x,y: x y, nums)
print(value)
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-22-e1612023858102.png" data-lazy- height="87" src="data:image/svg xml,” width=”477″>
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/python-lambda-23.png" data-lazy- height="91" src="data:image/svg xml,” width=”539″>
Conclusion
From this article, we have learned many aspects of the lambda function. Depending on what the program needs, we can use it and make better python coding. This is most commonly used to pass arguments to another function (for example, we have seen above functions map, filter, and reduce).
About the author
<img alt="Bamdeb Ghosh" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/IMG20181120103552-150×150.jpg" height="112" src="data:image/svg xml,” width=”112″>
Bamdeb Ghosh
Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He’s an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com