Lambda function with if else Elif in Python

Python python-loops

In this post, we are going to understand how to use the Lambda function with if-else Elif in Python with the help of examples. Lambda function with if-else, Python lambda without if-else, Python lambda with Multiple/nested if-else, Python lambda if-else with filter() function.

Python lambda function


The lambda function is a function without a name also known as an anonymous function defined by using the lambda keyword, it can have multiple arguments but only have one expression that is evaluated and returned. We write in single-line code.

Syntax

lambda arguments: expression

Program Example

In this example, we are using the lambda function to define an anonymous function result and later calling the function to get the result.

result = lambda val : val*val/val 
 
print(result(8)) 

#output 8.0

1. How to use Lambda function with if-else


The lambda function returns a value for every validated input. if-else are control statements. if block will execute when the condition is True and else block when the condition is False.

Syntax

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

Value1 will return when the condition is true and when the condition is false the value2 will return

Program Example

In this example, we will use if-else with the lambda function. It will return TRUE if the number is even else False.

result = lambda x : True if (x %2==0) else False

#calling the result anonymous function
print(result(16))
print(result(18))
print(result(19))

Output

True
True
False

2. Python Lambda conditional without if else


We can avoid the if-else block in the lambda function to get the same result as we did in the below example.

Program Example

result = lambda x :x %2==0

print(result(16))
print(result(18))
print(result(19))

Output

True
True
False

3.Python Lambda with if but without else


We can define lambda function with if, we can’t define without else block because we do not define what will return if the condition is False. We will have to use NONE to return nothing in from the else block. Let us understand how to use Python Lambda with if but without else.

Program Example

result = lambda x : x %2==0 if(x>0) else none

print(result(16))
print(result(18))
print(result(19))

Output

True
True
False

4. Python lamada with Mutiple/nested if-else


We can use the lambda function with multiple/nested. We can achieve this by using brackets with an if-else block. The syntax for this will be.

Syntax

lambda <arguments> : <return val1> if <condition1> else (<return val2> if <condition2> else <return val3>)

When condition1 is true then val1 will return else condition2 is checked if true then val2 will return, else val3 is returned. Let us understand with the help of an example how to use Python lambda with Multiple/nested if-else.

Program Example

result = lambda val :val%2 ==0 if val<10 else(val*3 if val>20 else val)


print(result(6))
print(result(7))
print(result(55))


Output

True
False
165

5.Python lambda if-else with filter() function


Python filter() function takes a sequence(list,tuple) and callback function as arguments.It execute callabck function for each of sequence and returns a new list of elements returns True.

Program Example

original_list = [2,4,6,12,38,32,36,40,26,0,-6]

resultlist = list(filter(lambda x : x %2 ==0 and x >0, original_list))
print('result list : ', resultlist)

Output

result list :  [2, 4, 6, 12, 38, 32, 36, 40, 26]

Summary

In this post, we have learned multiple ways of how to use Lambda function with if-else Elif in Python with code example and lambda with filter() function.