I am trying to return a boolean in a function like this:
return mylist and any(condition(x) for x in mylist)
The behavior should be to return True
if the list is empty or if any element in it meets the condition. I am using the first operand as a shortcircuit since any
would return True
if the list was empty, which is not what I am after.
I would expect [] and boolval
to return False
since the list is empty, but to my surprise it returns []
whether boolval
is True
or False
. I would expect the first operand to be automatically evaluated as a boolean since it is involved in a comparison operation, and not whatever is happening.
I am not really asking how to solve my problem, which is easily done by an explicit type conversion: bool(mylist)
, but rather asking what is happening and why.
edit: when I ask "why" this is happening I am not looking for the "facts" only, as they are already explained in the linked duplicate question, but also the reasons behind the implementation of this behavior.