any(), all() and empty iterable

John O'Hagan mail at
Sun Apr 12 18:53:51 CEST 2009

On Sun, 12 Apr 2009, Chris Rebert wrote:
> On Sat, Apr 11, 2009 at 9:00 PM, John O'Hagan <mail at> wrote:
> > Hi,
> >
> > I was getting some surprising false positives as a result of not
> > expecting this:
> >
> > all(element in item for item in iterable)
> >
> > to return True when 'iterable' is empty.


> This is justified in formal logic by the concept of vacuous truth. See
> all(P(x) for x in y) #Original Python statement
> ∀x∈y. P(x)  #equivalent in formal logic
> ¬¬[∀x∈y. P(x)]  #apply Double Negation
> ¬[∃x∈y. ¬P(x)]  #negate Universal Quantifier
> English interpretation: There does not exist a counterexample `x` in
> the set `y` which fails to satisfy predicate `P`.
> Comment: Indeed, in the case the set `y` is empty, there is no
> counterexample to be had, and thus both the final statement and the
> initial equivalent one are vacuously true. Yes, this may seem
> unintuitive at first. Logic is like that sometimes.
> Python equivalent to final statement: not any(not P(x) for x in y)
> Comment: Likewise, in the case of a empty `y`, any() returns False, so
> the code simplifies to `not False`, which obviously simplifies further
> to just True.
> So, if you agree with the behavior of any(), you must logically agree
> with the behavior of all(). ;-)

I do like this reasoning, but when you're testing for the ubiquity of 
something you don't expect to find it in an empty space - I was checking 
lists of sets to see which possible set members were in all the sets of a 
list, and found they all were: if the list contained no sets! I think that is 

Or to put it another way, if I ask someone "Amongst your books, is one of 
them 'Robinson Crusoe'?", and they don't have any books, they could 
answer 'yes' (or 'no' equally truthfully), but I'd rather they told me that 
no, they don't have  'Robinson Crusoe'.

Still, point taken, and quibble easily solved with a little extra test for 



More information about the Python-list mailing list