473,233 Members | 2,538 Online

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

Of course there is the always the iteration method:

list = [1, True, True, False, False, True]
status = True
for each in list:
status = status and each

but what is your best way to test for for False in a list?

Jul 8 '07 #1
6 1156
lex <al*************@gmail.comwrites:
list = [1, True, True, False, False, True]
status = True
for each in list:
status = status and each

but what is your best way to test for for False in a list?
status = all(list)
Jul 8 '07 #2
On 7/8/07, lex <al*************@gmail.comwrote:
Of course there is the always the iteration method:

list = [1, True, True, False, False, True]
status = True
for each in list:
status = status and each

but what is your best way to test for for False in a list?
In general, you can just do:

if something in list:
do_something()

--
Evan Klitzke <ev**@yelp.com>
Jul 9 '07 #3
On Sun, 2007-07-08 at 18:32 -0700, Evan Klitzke wrote:
On 7/8/07, lex <al*************@gmail.comwrote:
Of course there is the always the iteration method:

list = [1, True, True, False, False, True]
status = True
for each in list:
status = status and each

but what is your best way to test for for False in a list?

In general, you can just do:

if something in list:
do_something()
Caution! This tests whether at least one entry in 'list' is *equal* to
'something'. It is very unusual to want to test whether an object is
equal to True (or False). Usually one simply wants to know whether the
bool() value of that object is True (or False).

While it makes no difference with the contrived example above, in
general your suggestion may give unexpected results. The following
variant, making use of a generator expression to take the bool() of each
entry, will always work correctly:

if False in (bool(x) for x in list):
do_something()

But then again, in Python 2.5 you can use all() as suggested by Paul
Rubin:

if not all(list):
do_something()

I'd also like to point out, since nobody has so far, that 'list' is a
terrible name for a list because it shadows the name for the list type.

--
Carsten Haese
http://informixdb.sourceforge.net
Jul 9 '07 #4
On Jul 8, 7:43 pm, lex <alexander.so...@gmail.comwrote:
Of course there is the always the iteration method:

list = [1, True, True, False, False, True]
status = True
for each in list:
status = status and each

but what is your best way to test for for False in a list?

False in list

i.e. for a list N

if False in N:
# do something with N..

Jul 9 '07 #5
On Mon, 09 Jul 2007 06:21:31 +0300, Simon Forman <sa*******@gmail.com
wrote:
>
On Jul 8, 7:43 pm, lex <alexander.so...@gmail.comwrote:
>Of course there is the always the iteration method:

list = [1, True, True, False, False, True]
status = True
for each in list:
status = status and each

but what is your best way to test for for False in a list?

False in list

all() is slightly faster
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 9 '07 #6
On Mon, 2007-07-09 at 07:02 -0700, Paul McGuire wrote:
>any(map(lambda _ : _ is False,[3,2,1,0,-1]))
False
>any(map(lambda _ : _ is False,[3,2,1,0,-1,False]))
True
>>
Why the map/lambda? Is it faster than the more readable generator
expression?
>>any(x is False for x in [3,2,1,0,-1])
False
>>any(x is False for x in [3,2,1,0,-1,False])
True

--
Carsten Haese
http://informixdb.sourceforge.net
Jul 9 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.