You are currently viewing Python Lambda using if else

How to use an if else in Python lambda? You can use the if-else statement in a lambda as part of an expression. The lambda should have only one expression so here, it should be if-else. The if returns the body when the condition is satisfied, and the else body is returned when the condition is not satisfied. A Python lambda function is used to execute an anonymous function. This function can take any number of arguments, but can only have one expression and they can be used wherever function objects are required.

Advertisements

Note that the expression in the lambda function body should return some value. If the if-else expression does not return any value, the result from a lambda will be a None value.

1. Syntax of Lambda using if else

Following is the syntax of the using if-else statement on lambda expression.


# Syntax
lambda arguments,[argument,arguments] : <statement1> if <condition> else <statement2>

Here, statement1 is returned when if condition is True and statement2 when if condition is False.

2. Lambda using if else

You can use the if-else statement as part of the Python lambda expression. after evaluating an expression make sure it returns a value for both satisfied and unsatisfied conditions. if no value is returned, it returns the None value.


# Lambda function using if-else
min = lambda a, b : a if(a < b) else b
result = min(10, 20)
print("Finding the minimum of two numbers:",result)

Yields below output.

python lambda if else

Here, the lambda function will return a when if condition is true and return b when if condition is false.

3. Using Lambda inline with if-else

The same example above can be written as an inline invocation. Here, we surround the lambda function with parentheses and place the values for the arguments next to it enclosed within parentheses.


# Lambda function using if-else
min = (lambda a, b : a if(a < b) else b)(10,20)
print(min)

# Output:
# 10

Here, the lambda function min takes two arguments and returns the minimum value of the arguments.

4. Lambda with if else & else if (elif)

You can also use the nested if-else & else if statement in Python lambda expression. Remember that the lambda expression can take only one expression hence, you need to write this nested if else in a single expression.


# Lambda function using if else & else if
min = lambda a, b, c : f"{a} is smaller" if(a < b & b < c) \
     else f"{b} is smaller"  if (b < c) else f"{c} is smaller" 
print(min(40, 30, 10))

# Output:
# 10

Here, we pass three numbers to the lambda function, by using a nested if else statement as an expression, we return the value that is the smallest of three values.

Frequently Asked Questions on Python Lambda Using if else

What is a lambda function in Python?

A lambda function is an anonymous function defined using the lambda keyword. It can have any number of input parameters but can only contain a single expression.

How do you use if-else in a lambda function?

You can use the if-else statement within a lambda function to create a concise conditional expression. The syntax is: lambda arguments: expression_if_true if condition else expression_if_false.

How do I create a lambda function for finding the maximum of two numbers?

You can create a lambda function to find the maximum of two numbers using the max() function or an if-else statement within the lambda function.

What are some common use cases for lambda functions with if-else?

Common use cases include quick inline conditional checks, such as filtering elements in a list, transforming data based on conditions, or creating simple decision-making functions.

Is it considered good practice to use lambda functions with if-else?

While lambda functions can be useful for concise expressions, if-else conditions might make the code less readable. For complex conditions or operations, using regular functions may be a better choice.

Can a lambda function have multiple conditions?

A lambda function can only have a single expression. If you need to handle multiple conditions, you can use nested ternary expressions.

Conclusion

In this article, you have learned different examples of using if else & else if in Python lambda expression. You can use the if-else statement in a lambda as part of an expression. The lambda should have only one expression so here, it should be if-else. The if returns the body when the condition is satisfied, and the else body is returned when the condition is not satisfied.

Related Articles