I want to realize something like use lambda to call a function which has an output of 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, in this function it seems to need to call the function twice to find out whether 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 of func. But I am still curious about if there are some methods to call the function only once with lambda.
IndexErrorandTypeErrorexceptions that your code can throw, not everything else (including memory errors and keyboard interrupts).list = func(x,args); lambda x: list[0] if list else None