473,233 Members | 2,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,233 software developers and data experts.

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

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?

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.

Similar topics

21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
2
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
10
by: timor.super | last post by:
Hi all, Imagine I've an array of int : int anArray = new int; I want to extract all the integer that are superior to 500 I can do :
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.