Python Lambda


Introduction to Python Lambda

Lambda functions are one line function that does not have any name. These functions are created using the lambda keyword. Like regular functions, these functions can also accept arguments and return a value. Lambda functions are also known as anonymous functions.

How lambda function is declared:

lambda argument(s) : expression 

Here, arguments(s) are the number of parameters we want to pass, and the expression contains the statement to evaluate. The return keyword is not used, the result of the expression is automatically returned.

Lambda functions can take any number of arguments and can return only one value.

Lambda without argument.

In [1]:
a = lambda : print("Logical Python")
a()
Logical Python

Lambda with one argument.

In [2]:
a = lambda b : b*5

print(a(5))
25

Lambda with multiple arguemnts. Returns result in r.

In [3]:
multiply = lambda x,y,z : x*y*z

result = multiply (2,3,4)

print(result)
24

Lambda with if-else

The lambda function can be easily used with the if-else statement. In the below example, we will check if the number is odd or even using the lambda and if-else. This is a basic example of how we can use the lambda to make a one-liner to check odd or even.

In [1]:
odd_even = lambda x : "even" if x % 2 == 0 else "odd"

print(odd_even(7))

print(odd_even(8))
odd
even

Lambda with List Comprehension

In this example, we will use the list comprehension to with lambda to multiply numbers by 2. For loop will iterate over the numbers, and the lambda function will multiply each number, stored in variable “num” with 2. The variable “new_numbers” stored the list of lambda function objects. Again, the for loop is used to iterate over this list and call the function object using “number()”.

In [1]:
numbers = [1,2,3,4,5]

new_numbers = [lambda num=x : num*2 for x in numbers]

for number in new_numbers:
    print(number())
2
4
6
8
10

Lambda with filter() function

The filter() function is used to filter out the elements from the sequence. It will only return the items for which the expression returns true. The lambda function can be used with the filter for the quick function inside the filter.

For example, in the below code, we are filtering out the even numbers from the numbers list by creating the one-liner using the lambda function. It is a quick way to do this because we need not to separately define the function using the def keyword.

In [1]:
numbers = [1,98,2,45,69,92,3,45,75]

even_numbers  = list(filter(lambda a : (a %2 ==0), numbers))

print(even_numbers)

Lambda with map() function

The map() function is used to map the items of the sequence with the function. It returns the modified list of the items.

In the below example, we will create the lambda function to find out the square of the numbers and map that function with the numbers list.

In [1]:
numbers = [1,2,3,4,5]

square = list(map(lambda x : x **2, numbers))

print(square)
[1, 4, 9, 16, 25]

Lambda with reduce() function

The reduce function is used for the rolling computations on the sequence. It performs the repetitive operation in the pairs. It returns only a single value, not the sequence of the values.

In the below example, lambda and reduce are used to find out the sum of the numbers. The lambda below acts as the function to add two numbers, and the reduce() function calls this lambda again and again for each pair.

In [1]:
from functools import reduce

numbers = [1,2,3,4,5]

sum = reduce(lambda a, b : a+b, numbers)

print(sum)
15

Advantages of Lambda Functions.

  1. Lambda functions are fast and save time when a function is required for the temporary short task.
  2. Creates one-liner functions where the operation is so small that we need not write the full function with the def keyword.
  3. More pythonic code, especially when used with the map(), filter, and reduce() functions. Lambda is passed as the argument instead of the regular function, which makes it more pythonic.

Difference Between Lambda and Regular functions

LambdaRegular Functions
Best for performing short temporary operations.Good for cases where multiple lines are required.
Sometimes it is tough to understand code written using lambda functions.Easy to read and understand.
Supports one-liner code.Any number of lines.
Easy to debug the code.Much tougher to debug.

References

  1. Lambda expressions in Python
  2. Python Lambda Functions