any(), all() and empty iterable

John O'Hagan mail at johnohagan.com
Sun Apr 12 00:00:40 EDT 2009


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. 

I guess it goes into hairy Boolean territory trying to decide if an element is 
in an item that doesn't exist (if that's what's happening), but I would have 
thought not. It seems inconsistent with the behaviour of

any(element in item for item in iterable)

which returns False when 'iterable' is empty.

Sorry if this has come up before, but 'any' and 'all' make for fruitless 
googling!

Any light to be shed?

Regards,

John



More information about the Python-list mailing list