Python lambda call function with None output

Viewed 1453

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.

1 Answers
Related