In this post, we will learn the Lambda function with if else Elif in Python and how to use the Lambda function with if-else Elif in Python with the help of examples. we will also cover 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 first define an anonymous function myfun that takes an argument and later calls this function to get the Result output.
#Lambda function with if else Elif in Python
myfun = lambda val : val*val/val
print(myfun(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 simply define a lambda anonymous function and call it without the if-else block in the lambda function to get the same result. Let us understand with the below example.
Program Example
myresult = lambda x :x %2==0
print(myresult (16))
print(myresult (18))
print(myresult (19))
Output
True
True
False
3.Python Lambda with if but without else
We can define the anonymous lambda function with an IF statement, we can’t define it without an 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 lambda 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 the Lambda function with if else Elif in Python with code example and lambda with filter() function.