What is the most efficient way to test for False in a list?

Duncan Booth duncan.booth at invalid.invalid
Tue Jul 10 03:20:02 EDT 2007


Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote:

> "Diez B. Roggisch" <deets at nospam.web.de> writes:
>> >> status = all(list)
>> > 
>> > Am I mistaken, or is this no identity test for False at all?
>> 
>> You are mistaken. all take an iterable and returns if each value of
>> it is true.
> 
> all(list) does what the OP's code did, tests for the presence of a
> false value in the list.  If you want an identity test, use 
> 
>   status = not (False in list)

That is an equality test, not an identity test:

>>> False in [0]
True

as others have said, if you need an identity test use:

 any(x is False for x in theList)



More information about the Python-list mailing list