I feel both all
and any
are opportunities slightly wasted in Python, although I have some idea of why they couldn’t work any better.
It would be so useful if any((a, b, c, d, ...))
meant the same as a or b or c or d or ...
but it doesn’t — quite. It actually returns bool(a or b or c or d or ...)
. Exactly the same is true for all and and.
So you need to write something like
next((i for i in (a, b, ...) if i), False)
to get the “first non-empty” part of any
and something even worse for all
.
Why is it like this? My guess is so that empty all(())
and any(())
have logical and consistent meanings with the non-empty versions. For example, you really want all(())
to be True
but True
is not in fact an element in ()
.