473,396 Members | 1,935 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,396 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 1161
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 :
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.