Python : How to use if, else and elif in Lambda Functions

Lambda function is the function that is anonymous. This anonymous functions are defined using a lambda keyword.

In this article we are going to  discuss about how to use if , else and elif in a lambda functions.

Syntax of Lambda function:

lambda arguments: expression

So this function can have any number of parameter but have only one expression.That expression should return.

Here is an example how we are using Lambda function:

# lambda.py

cube = lambda x: x * x * x

print(cube(11))

Output:

lambda.py

1331

How to use if-else in Lambda function:

Here we are going to use if-else in Lambda function.

lambda <arguments> : <Return Value if condition is True> if <condition> else <Return Value if condition is False>

Let’s take an example, Create  lambda function to check if the given value is between 11 to 22.

# lamda.py

verify = lambda x: True if (x > 9 and x < 18) else False
print(verify(5))
print(verify(17))
print(verify(21))

Output:

RESTART: C:/Users/HP/Desktop/lambda.py
False
True
False

 Conditional lambda function without if-else in python:

Without using if and else keyword we can still get our result using conditional lambda function.For example,

# lambda.py

verify = lambda x : x > 9 and x < 18
print(verify(5))
print(verify(17))
print(verify(21))

Output:

RESTART: C:/Users/HP/Desktop/lambda.py

False
True
False

So we have seen that we can get our result without using if-else statement.

Using filter() function with the conditional lambda function:

This function get a callback() function and a list of elements. It repeats over each and every elements in list and calls the callback() function on every element. If callback() returns True then it will add in new list .In the end, returns the list of items.

# lambda.py

listA = [12, 28, 23, 17, 9, 29, 50]

listOutput = list(filter(lambda x: x > 9 and x < 20, listA))
print(listOutput)

Output:

RESTART: C:/Users/HP/Desktop/lambda.py
[12, 17]

Here we have used lambda function to filter elements and in the end returns list of elements that lies between 9 to 20.

Using if, elif & else in a lambda function:

We can not always depend on if else in a lambda function,sometime there are some possibilities when we need to check on multiple conditions.We can’t directly use else if in a lambda function. But we can fo the same by using if else & brackets .

Syntax:

lambda <args> : <return Value> if <condition > ( <return value > if <condition> else <return value>)

Lets take an example:

lambda.py

converter = lambda x : x*2 if x < 11 else (x*3 if x < 22 else x)
print(converter(5))
print(converter(24))

Output:

lambda.py

10
24

So, in the above example, 5 * 2 = 10, which is less than 11. So, it gives10 because given condition is true. In the case of 24, condition is false; that is why it returns 24 as .

Conclusion:

So in this article we have seen how to use if , else and elif in a lambda functions.We have also seen how to use conditional statement and use of filter  in lambda function.