I was writing some lambda functions and couldn't figure this out. Is there a way to have something like lambda x: x if [x b] else b
works ok. So far lambda x: x < 3 and x or None
seems to be the closest i have found.
silpol
30812 silver badges28 bronze badges
asked Oct 3, 2012 at 13:00
A lambda, like any function, must have a return value.
lambda x: x if [x data = [1, 2, 5, 10, -1]
>>> filter[lambda x: x < 3, data]
[1, 2, -1]
The implementation is different in 2.x and 3.x: while 2.x provides a list, 3.x provides an iterator. Using a list comprehension might make for a cleaner use in 3.x:
>>> data = [1, 2, 5, 10, -1]
>>> [filter[lambda x: x < 3, data]]
[1, 2, -1]
answered Jan 10, 2017 at 19:17
3
What's wrong with lambda x: x if x < 3 else None
?
answered Oct 3, 2012 at 13:04
user4815162342user4815162342
129k15 gold badges253 silver badges315 bronze badges
6
You can always try to invoke 'filter' for conditional checks. Fundamentally, map[]
has to work on
every occurrence of the iterables, so it cannot pick and choose. But filter may help narrow down the choices. For example, I create a list from 1 to 19 but want to create a tuple of squares of only even numbers.
x = list[range[1,20]]
y = tuple[map[lambda n: n**2, filter[lambda n: n%2==0,x]]]
print [y]
answered Apr 7, 2017 at 23:51
TirthaTirtha
4924 silver badges7 bronze badges
You can use ellipsis ...
to fill else
statement
lambda x: x if [x 10 and x < 20] else False
Here we are using if else in a lambda function, if given value is between 10 to 20 then it will return True else it will return False. Now let’s use this function to check some values i.e.
# Lambda function to check if a given vaue is from 10 to 20. test = lambda x : True if [x > 10 and x < 20] else False # Check if given numbers are in range using lambda function print[test[12]] print[test[3]] print[test[24]]
Output:
True False False
Creating conditional lambda function without if else
Well using ‘if’ ‘else’ keywords makes things easy to understand, but in lambda we can avoid using if
& else keywords and still achieve same results. For example let’s modify the above created lambda function by removing if else keywords & also True False i.e.
lambda x : x > 10 and x < 20
This lambda function does the same stuff as above i..e checks if given number lies between 10 to 20. Now let’s use this function to check some values i.e.
# Lambda function to check if a given vaue is from 10 to 20. check = lambda x : x > 10 and x < 20 # Check if given numbers are in range using lambda function print[check[12]] print[check[3]] print[check[24]]
Output:
True False False
Using filter[] function with a conditional lambda function [with if else]
filter[] function accepts
a callback[] function and a list of elements. It iterates over all elements in list and calls the given callback[] function
on each element. If callback[] returns True then it appends that element in the new list. In the end it returns a new list of filtered elements only.
Suppose we have a list of numbers i.e.
# List of numbers listofNum = [1,3,33,12,34,56,11,19,21,34,15]
Now let’s use filter[] function to filter numbers between 10 to 20 only by passing a conditional lambda function [with if else] to it i.e.
# Filter list of numbers by keeping numbers from 10 to 20 in the list only listofNum = list[filter[lambda x : x > 10 and x < 20, listofNum]] print['Filtered List : ', listofNum]
Output:
Filtered List : [12, 11, 19, 15]
it uses the passed lambda function to filter elements and in the end returns list of elements that lies between 10 to 20,
Advertisements
Using if, elif & else in a lambda function
Till now we have seen how to use if else in a lambda function but there might be cases when we need to check multiple conditions in a lambda function. Like we need to use if , else if & else in a lambda function. We can not directly use elseif in a
lambda function. But we can achieve the same effect using if else & brackets i.e.
lambda : if [ if else ]
Let’s see how to do that,
Create a lambda function that accepts a number and returns a new number based on this logic,
- If the given value is less than 10 then return by multiplying it by 2
- else if it’s between 10 to 20 then return multiplying it by 3
- else returns the same un-modified value
# Lambda function with if, elif & else i.e. # If the given value is less than 10 then Multiplies it by 2 # else if it's between 10 to 20 the multiplies it by 3 # else returns the unmodified same value converter = lambda x : x*2 if x < 10 else [x*3 if x < 20 else x]
Let’s use this lambda function,
print['convert 5 to : ', converter[5]] print['convert 13 to : ', converter[13]] print['convert 23 to : ', converter[23]]
Output:
convert 5 to : 10 convert 13 to : 39 convert 23 to : 23
Complete example is as follows,
def main[]: print['*** Using if else in Lambda function ***'] # Lambda function to check if a given vaue is from 10 to 20. test = lambda x : True if [x > 10 and x < 20] else False # Check if given numbers are in range using lambda function print[test[12]] print[test[3]] print[test[24]] print['*** Creating conditional lambda function without if else ***'] # Lambda function to check if a given vaue is from 10 to 20. check = lambda x : x > 10 and x < 20 # Check if given numbers are in range using lambda function print[check[12]] print[check[3]] print[check[24]] print['*** Using filter[] function with a conditional lambda function [with if else] ***'] # List of numbers listofNum = [1,3,33,12,34,56,11,19,21,34,15] print['Original List : ', listofNum] # Filter list of numbers by keeping numbers from 10 to 20 in the list only listofNum = list[filter[lambda x : x > 10 and x < 20, listofNum]] print['Filtered List : ', listofNum] print['*** Using if, elif & else in Lambda function ***'] # Lambda function with if, elif & else i.e. # If the given value is less than 10 then Multiplies it by 2 # else if it's between 10 to 20 the multiplies it by 3 # else returns the unmodified same value converter = lambda x : x*2 if x < 10 else [x*3 if x < 20 else x] print['convert 5 to : ', converter[5]] print['convert 13 to : ', converter[13]] print['convert 23 to : ', converter[23]] if __name__ == '__main__': main[]
Output:
*** Using if else in Lambda function *** True False False *** Creating conditional lambda function without if else *** True False False *** Using filter[] function with a conditional lambda function [with if else] *** Original List : [1, 3, 33, 12, 34, 56, 11, 19, 21, 34, 15] Filtered List : [12, 11, 19, 15] *** Using if, elif & else in Lambda function *** convert 5 to : 10 convert 13 to : 39 convert 23 to : 23