Re: What way is the best to check an empty list?

2009-03-26 Thread Niklas Norrthon
On 26 Mar, 04:31, Steve Holden st...@holdenweb.com wrote: Stef Mientki wrote: Now it would be nice to allow iteration over others too, like None .    a = None    for item in a :          do_something_with_item To me that makes about as much sense as writing     for x in 1.0:        

Re: What way is the best to check an empty list?

2009-03-26 Thread Niklas Norrthon
On 26 Mar, 08:18, Niklas Norrthon niklas.norrt...@hotmail.com wrote: But that can easily be achieved with the or operator as Michiel Overton notes elsewhere in this thread: Michiel Overtoom was what I meant to write. My apologies! def some_function(arg, coll=None):     do_stuff(arg)    

What way is the best to check an empty list?

2009-03-25 Thread srinivasan srinivas
For ex: to check list 'A' is empty or not.. if A == []: if A.count == 0: if len(A) == 0: if not A: Thanks, Srini Add more friends to your messenger and enjoy! Go to http://messenger.yahoo.com/invite/ -- http://mail.python.org/mailman/listinfo/python-list

Re: What way is the best to check an empty list?

2009-03-25 Thread Andre Engels
On Wed, Mar 25, 2009 at 3:38 PM, srinivasan srinivas sri_anna...@yahoo.co.in wrote: For ex: to check list 'A' is empty or not.. if A == []: if A.count == 0: if len(A) == 0: if not A: I would go for the last one, because it has the highest likelihood of doing what is intended when fed with

Re: What way is the best to check an empty list?

2009-03-25 Thread Tim Chase
srinivasan srinivas wrote: For ex: to check list 'A' is empty or not.. if A == []: if A.count == 0: if len(A) == 0: if not A: if not A -tkc -- http://mail.python.org/mailman/listinfo/python-list

Re: What way is the best to check an empty list?

2009-03-25 Thread bearophileHUGS
srinivasan srinivas: For ex: to check list 'A' is empty or not.. Empty collections are false: if somelist: ... # somelist isn't empty else: ... # somelist is empty Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: What way is the best to check an empty list?

2009-03-25 Thread John Machin
On Mar 26, 1:38 am, srinivasan srinivas sri_anna...@yahoo.co.in wrote: Depends on what you mean by best; like graduation day at kindergarten, everyone gets a prize: For ex: to check list 'A' is empty or not.. if A == []: most obviously correct if A.count == 0: best use of imagination if

Re: What way is the best to check an empty list?

2009-03-25 Thread andrew cooke
i will go against the grain slightly and say that len is probably the best compromise in most situations (although i admit i don't know what count is) because i think it will work when you expect it to and break when you have a bug in your program. using a simple boolean is more robust (and what

Re: What way is the best to check an empty list?

2009-03-25 Thread Andre Engels
On Wed, Mar 25, 2009 at 4:21 PM, andrew cooke and...@acooke.org wrote: i will go against the grain slightly and say that len is probably the best compromise in most situations (although i admit i don't know what count is) because i think it will work when you expect it to and break when you

Re: What way is the best to check an empty list?

2009-03-25 Thread John Machin
On Mar 26, 2:21 am, andrew cooke and...@acooke.org wrote: i will go against the grain slightly and say that len is probably the best compromise in most situations (although i admit i don't know what count is) because i think it will work when you expect it to and break when you have a bug in

Re: What way is the best to check an empty list?

2009-03-25 Thread Raymond Hettinger
On Mar 25, 7:38 am, srinivasan srinivas sri_anna...@yahoo.co.in wrote: For ex: to check list 'A' is empty or not.. if A == []: if A.count == 0: if len(A) == 0: if not A: PEP 8 recommends the latter. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: What way is the best to check an empty list?

2009-03-25 Thread andrew cooke
Andre Engels wrote: On Wed, Mar 25, 2009 at 4:21 PM, andrew cooke and...@acooke.org wrote: i will go against the grain slightly and say that len is probably the best compromise in most situations (although i admit i don't know what [...] but i may be wrong - are there any containers (apart

Re: What way is the best to check an empty list?

2009-03-25 Thread Stef Mientki
andrew cooke wrote: Andre Engels wrote: On Wed, Mar 25, 2009 at 4:21 PM, andrew cooke and...@acooke.org wrote: i will go against the grain slightly and say that len is probably the best compromise in most situations (although i admit i don't know what [...] but i may be

Re: What way is the best to check an empty list?

2009-03-25 Thread Martin P. Hellwig
Raymond Hettinger wrote: On Mar 25, 7:38 am, srinivasan srinivas sri_anna...@yahoo.co.in wrote: For ex: to check list 'A' is empty or not.. if A == []: if A.count == 0: if len(A) == 0: if not A: PEP 8 recommends the latter. Raymond I can't seem to find where this recommendation is

Re: What way is the best to check an empty list?

2009-03-25 Thread andrew cooke
Martin P. Hellwig wrote: Raymond Hettinger wrote: On Mar 25, 7:38 am, srinivasan srinivas sri_anna...@yahoo.co.in wrote: if not A: PEP 8 recommends the latter. I can't seem to find where this recommendation is mentioned or implied. it's the next-to-last sub-item, just before the references.

Re: What way is the best to check an empty list?

2009-03-25 Thread Albert Hopkins
On Wed, 2009-03-25 at 21:26 +, Martin P. Hellwig wrote: PEP 8 recommends the latter. Raymond I can't seem to find where this recommendation is mentioned or implied. Wow, you must not have looked very hard: 1. Point your browser to http://www.python.org/dev/peps/pep-0008/

Re: What way is the best to check an empty list?

2009-03-25 Thread Michiel Overtoom
On 25 Mar 2009, at 21:29 , Stef Mientki wrote: Now it would be nice to allow iteration over others too, like None . a = None for item in a : do_something_with_item I saw this technique used in CherryPy: a=None for item in a or []: ...print item ... a=[1,2,3] for

Re: What way is the best to check an empty list?

2009-03-25 Thread Martin P. Hellwig
Albert Hopkins wrote: On Wed, 2009-03-25 at 21:26 +, Martin P. Hellwig wrote: PEP 8 recommends the latter. Raymond I can't seem to find where this recommendation is mentioned or implied. Wow, you must not have looked very hard: 1. Point your browser to

Re: What way is the best to check an empty list?

2009-03-25 Thread Carl Banks
On Mar 25, 1:19 pm, andrew cooke and...@acooke.org wrote: actually, the implication of what you said is probably worth emphasising to the original poster: often you don't need to test whether a list is empty or not, you simply iterate over its contents:   for x in foo:     # do something

Re: What way is the best to check an empty list?

2009-03-25 Thread Carl Banks
On Mar 25, 8:27 am, Andre Engels andreeng...@gmail.com wrote: On Wed, Mar 25, 2009 at 4:21 PM, andrew cooke and...@acooke.org wrote: but i may be wrong - are there any containers (apart from pathological hand-crafted examples) that would not define __len__()? When writing my answer, I

Re: What way is the best to check an empty list?

2009-03-25 Thread Carl Banks
On Mar 25, 7:38 am, srinivasan srinivas sri_anna...@yahoo.co.in wrote: For ex: to check list 'A' is empty or not.. if A == []: if A.count == 0: if len(A) == 0: if not A: PEP 8 recommends the last one, and most Pythonistas here probably would as well, so that is probably what you should do if

What way is the best to check an empty list?

2009-03-25 Thread srinivasan srinivas
For ex: to check list 'A' is empty or not.. if A == []: if A.count == 0: if len(A) == 0: if not A: Connect with friends all over the world. Get Yahoo! India Messenger at http://in.messenger.yahoo.com/?wm=n/ -- http://mail.python.org/mailman/listinfo/python-list

Re: What way is the best to check an empty list?

2009-03-25 Thread Josh Dukes
I believe if not A: is the most pythonic, but depending on what you're doing a list might not be the right thing to use at all. A little while ago I got some help from this malining list in dealing with a situation where lists really were not efficient (finding prime numbers...for fun). In my case

Re: What way is the best to check an empty list?

2009-03-25 Thread Steve Holden
Stef Mientki wrote: andrew cooke wrote: Andre Engels wrote: On Wed, Mar 25, 2009 at 4:21 PM, andrew cooke and...@acooke.org wrote: i will go against the grain slightly and say that len is probably the best compromise in most situations (although i admit i don't know what

Re: What way is the best to check an empty list?

2009-03-25 Thread Steven D'Aprano
On Wed, 25 Mar 2009 21:26:10 +, Martin P. Hellwig wrote: Raymond Hettinger wrote: On Mar 25, 7:38 am, srinivasan srinivas sri_anna...@yahoo.co.in wrote: For ex: to check list 'A' is empty or not.. if A == []: if A.count == 0: if len(A) == 0: if not A: ... Personally I would go for