checking if a list is empty

James Mills prologic at shortcircuit.net.au
Mon May 9 00:46:45 EDT 2011


On Mon, May 9, 2011 at 2:34 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> "bool(list)" describes whether the list contains something.  "Not"
> being a logical operator, it stands to reason that "not list" should
> mean the same thing as "not bool(list)".  Anything else would be
> surprising and pointlessly convoluted.  If "not list" bothers you,
> then I would suggest that your problem is actually with "bool(list)".

I concur! +1

>>        ...  should return True only if 'list' is bound to NULL, or if the
>> name 'list' would throw a name exception...

[...]

>>        ...  and, Python should have an explicit way of testing for an empty
>> list that is clear, concise, and easy to understand even for the relatively
>> new Python programmer.  This is not intended to generate counter argument,
>> its just my feedback on a language construct that I find to be inconsistent
>> logically. ... Please, no pedantic discourse explaining why I'm wrong...
>> just put in into a PEP and move on.  :)
>
> It does.  "if len(list) == 0"

Again +1 on the if len(list) == 0:

Now... One thing that really concerns me about the Python
community as a whole is the no. of varying opinions of
"correct" ways of doing things and the distaste for a lot of
things (which in Python are just obvious).

If you are an opinionated individuals and disagree with the
suggestion (even if it is "right"), perhaps keep that to yourself.

if not my_list:

is a perfectly valid and fine idiom to use in Python.

If you prefer some other way, that's fine. Quite frankly
I'm sick of seeing posts that argue for the sake of arguing.

cheers
James

-- 
-- James Mills
--
-- "Problems are solved by method"



More information about the Python-list mailing list