Python lambda call function with output None
I want to implement something like use lambda
to call a function that has the output None or list and if it is a list then get the first value.
lambda x: func(x,args)[0] if func(x,args) is not None else None
However, this function seems to need to call the function twice to find out if it is None. Of course, I can just write the code using try or conditional statements:
def function(x):
try:
return func(x,args)[0]
except (IndexError,TypeError):
return None
or just change the output func
. But I'm still wondering if there are any methods to call the function only once with lambda
.
+3
source to share