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

L

lex

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?
 
P

Paul Rubin

lex said:
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)
 
E

Evan Klitzke

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()
 
C

Carsten Haese

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.
 
S

Simon Forman

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..
 
D

Daniel

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
 
B

Bjoern Schliessmann

Paul said:
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)

Am I mistaken, or is this no identity test for False at all?

Regards,


Björn
 
D

Diez B. Roggisch

Bjoern said:
Paul said:
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)

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.

Diez
 
H

Hrvoje Niksic

Diez B. Roggisch said:
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
 
P

Paul McGuire

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
 
H

Hrvoje Niksic

Paul McGuire said:
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;

I'm aware of that, I simply pointed out that "False in list" and
any(list) are not equivalent and where the difference lies.
any(map(lambda _ : _ is False,[3,2,1,0,-1]))

Note that you can use itertools.imap to avoid the unnecessary
intermediate list creation. Even better is to use a 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
 
P

Paul Rubin

Diez B. Roggisch said:
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)
 
B

Bjoern Schliessmann

Diez said:
You are mistaken. all take an iterable and returns if each value
of it is true.

That's an identity test for True, not for False (the latter was
requested). Thus, I'm not mistaken.

Regards,


Björn
 
P

Paul Rubin

Bjoern Schliessmann said:
That's an identity test for True, not for False (the latter was
requested). Thus, I'm not mistaken.

No, "true" is not the same thing as "True".
 
B

Bjoern Schliessmann

Paul said:
Bjoern Schliessmann <[email protected]>
writes:

No, "true" is not the same thing as "True".

Oops, read over that case. Still: No identity test for False.

Regards,


Björn

--
BOFH excuse #145:

Flat tire on station wagon with tapes. ("Never underestimate the
bandwidth of a station wagon full of tapes hurling down the
highway" Andrew S. Tannenbaum)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top