checking if a list is empty

Ian Kelly ian.g.kelly at gmail.com
Mon May 9 00:34:04 EDT 2011


On Sun, May 8, 2011 at 9:33 PM, harrismh777 <harrismh777 at charter.net> wrote:
>   Why should the negation of a list imply that the list empty?  ... nor any
> other abstract condition which is not well suited to 'not' ? (forget python
> for a moment... then move on to my argument...)
>
>   What made the python development team (or individual) decide that the
> logical construct   'not list'   would return True if the list is empty?
> To say that "This is the way we do it in Python because Python does it this
> way"---  goes beyond pedantic becoming circular and redundant.
>
>   On the other hand, if you were to tell me *why* you think that the logical
> construct 'not list' should be logically True when the list is empty--- when
> clearly negating a list would not necessarily mean to make the list empty
> (including destroying it)... then you might be seeing my point.

"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)".

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

Why?  Should "not False" return True only if "False" is bound to NULL
(whatever you mean by that), or if the name "False" 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"



More information about the Python-list mailing list