I would like to create a lambda that raises ValueError
if its input is a non-empty list. Based on the answer proposed here, I implemented it as follows:
f = lambda x: (_ for _ in ()).throw(ValueError()) if len(x) != 0 else []
(Note that the return value []
when x
is an empty list is not important here, I just had to put something to have a complete if
-else
statement.)
That works as intended, but the code looks a bit cryptic. Is there any nicer way to write the above? I am thinking about using x
somehow in the generator (_ for _ in ())
but am not able to see straightforwardly how to do so in order to get the desired result.