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

Paul McGuire ptmcg at austin.rr.com
Mon Jul 9 16:02:33 CEST 2007


On Jul 9, 7:39 am, Hrvoje Niksic <hnik... at xemacs.org> wrote:
> "Diez B. Roggisch" <d... at nospam.web.de> writes:
>
>
>
> >>>> but what is your best way to test for for False in a list?
> [...]
> >>> 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.
>
> Testing for truth is not the same as an identity test for False.  OP's
> message doesn't make it clear which one he's looking for.  This
> illustrates the difference:
>
> >>> False in [3, 2, 1, 0, -1]
>
> True    # no False here>>> all([3, 2, 1, 0, -1])
>
> False   # false value present, not necessarily False

I think if you want identity testing, you'll need to code your own;
here's a map+lambda way:

>>> any(map(lambda _ : _ is False,[3,2,1,0,-1]))
False
>>> any(map(lambda _ : _ is False,[3,2,1,0,-1,False]))
True
>>>

-- Paul




More information about the Python-list mailing list