Open In App

Lambda with if but without else in Python

Last Updated : 06 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, Lambda function is an anonymous function, which means that it is a function without a name. It can have any number of arguments but only one expression, which is evaluated and returned. It must have a return value.

Since a lambda function must have a return value for every valid input, we cannot define it with if but without else as we are not specifying what will we return if the if-condition will be false i.e. its else part.

Let’s understand this with a simple example of lambda function to square a number only if it is greater than 0 using if but without else.

Example #1:

Python3




# Lambda function with if but without else.
square = lambda x : x*x if(x > 0)
 
print(square(6))


Output:

File "/home/2c8c59351e1635b6b026fb3c7fc17c8f.py", line 2
    square = lambda x : x*x if(x > 0)
                                    ^
SyntaxError: invalid syntax

The above code on execution shows SyntaxError, as we know that a lambda function must return a value and this function returns x*x if x > 0 and it does not specify what will be returned if the value of x is less than or equal to 0.

To correct it, we need to specify what will be returned if the if-condition will be false i.e. we must have to specify its else part.

Let’s see the above code with its else part.

Code:

Python3




# Lambda function with if-else
square = lambda x : x*x if(x > 0) else None
 
print(square(4))


Output:

16

Example #2: The first code is with if but without else then second is with if-else.

Python3




# Example of lambda function using if without else
mod = lambda x : x if(x >= 0)
 
print(mod(-1))


Output:

File "/home/20b09bdd29e24dfe24c185cd99dfcdfa.py", line 2
    mod = lambda x : x if(x >= 0)
                                ^
SyntaxError: invalid syntax

Now, let’s see it using if-else.

Python3




# Example of lambda function using if-else
mod = lambda x : x if(x >= 0) else -x
 
print(mod(-1))


Output:

1

Example #3: The first code is with if but without else then second is with if-else.

Python3




# Example of lambda function using if without else
max = lambda a, b : x if(a > b)
 
print(max(1, 2))


Output:

File "/home/8cf3856fc13d0ce75edfdd76793bdde4.py", line 2
    max = lambda a, b : x if(a > b)
                                  ^
SyntaxError: invalid syntax

Now, let’s see it using if-else.

Python3




# Example of lambda function using if-else
max = lambda a, b : a if(a > b) else b
 
print(max(1, 2))
print(max(10, 2))


Output:

2
10


Previous Article
Next Article

Similar Reads

How to use if, else & elif in Python Lambda Functions
Lambda function can have multiple parameters but have only one expression. This one expression is evaluated and returned. Thus, We can use lambda functions as a function object. In this article, we will learn how to use if, else & elif in Lambda Functions. Using if-else in lambda function The lambda function will return a value for every valida
2 min read
Python: Iterating With Python Lambda
In Python, the lambda function is an anonymous function. This one expression is evaluated and returned. Thus, We can use lambda functions as a function object. In this article, we will learn how to iterate with lambda in python. Syntax: lambda variable : expression Where, variable is used in the expressionexpression can be an mathematical expressio
2 min read
Python Program that Displays the Letters that are in the First String but not in the Second
Python program to display the letters in the first string but not in the second can be done by taking two sets and subtracting them. As sets support the difference operator, one set could contain the letters of the first string and another set could contain the letters of the second string and when subtracted we could obtain the desired result. Exa
5 min read
Ways to sort list of dictionaries by values in Python - Using lambda function
In this article, we will cover how to sort a dictionary by value in Python. Sorting has always been a useful utility in day-to-day programming. Dictionary in Python is widely used in many applications ranging from competitive domain to developer domain(e.g. handling JSON data). Having the knowledge to sort dictionaries according to their values can
2 min read
Lambda and filter in Python Examples
Prerequisite : Lambda in Python Given a list of numbers, find all numbers divisible by 13. Input : my_list = [12, 65, 54, 39, 102, 339, 221, 50, 70] Output : [65, 39, 221] We can use Lambda function inside the filter() built-in function to find all the numbers divisible by 13 in the list. In Python, anonymous function means that a function is witho
2 min read
Map function and Lambda expression in Python to replace characters
Given a string S, c1 and c2. Replace character c1 with c2 and c2 with c1. Examples: Input : str = 'grrksfoegrrks' c1 = e, c2 = r Output : geeksforgeeks Input : str = 'ratul' c1 = t, c2 = h Output : rahul We have an existing solution for this problem in C++. Please refer to Replace a character c1 with c2 and c2 with c1 in a string S. We can solve th
2 min read
Python | Find the Number Occurring Odd Number of Times using Lambda expression and reduce function
Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time & constant space. Examples: Input : [1, 2, 3, 2, 3, 1, 3] Output : 3 We have existing solution for this problem please refer Find the Number Occurring Odd Number of Times link. we will solv
1 min read
Intersection of two arrays in Python ( Lambda expression and filter function )
Given two arrays, find their intersection. Examples: Input: arr1[] = [1, 3, 4, 5, 7] arr2[] = [2, 3, 5, 6] Output: Intersection : [3, 5] We have existing solution for this problem please refer Intersection of two arrays link. We will solve this problem quickly in python using Lambda expression and filter() function. Implementation: C/C++ Code # Fun
1 min read
Overuse of lambda expressions in Python
What are lambda expressions? A lambda expression is a special syntax to create functions without names. These functions are called lambda functions. These lambda functions can have any number of arguments but only one expression along with an implicit return statement. Lambda expressions return function objects. For Example consider the lambda expr
8 min read
Using lambda in GUI programs in Python
Python Lambda Functions are anonymous function means that the function is without a name. In this article, we will learn to use lambda functions in Tkinter GUI. We use lambda functions to write our code more efficiently and to use one function many times by passing different arguments. We use the lambda function to pass arguments to other functions
3 min read
Article Tags :
Practice Tags :