Re: checking if a list is empty



On May 5, 11:36 pm, Jabba Laci <jabba.l...@xxxxxxxxx> wrote:
Hi,

If I want to check if a list is empty, which is the more pythonic way?

li = []

(1) if len(li) == 0:
...
or
(2) if not li:

The Python core developers use the second form.
See http://www.python.org/dev/peps/pep-0008/
for the official recommendation.


Raymond

.



Relevant Pages

  • Re: while c = f.read(1)
    ... The problem with interpreting empty as false is that empty ... > If Python is too "wild" for your taste, ... In that case you wouldn't return an empty sequence if you wanted ... a false value in a sequence context but would throw an exception. ...
    (comp.lang.python)
  • Ply(LALR) and Yacc behaving differently
    ... I am trying to implement a small compiler in python and, ... Perhaps this belongs on some compiler list, but I couldn't decide if it ... Empty ... def p_Block: ...
    (comp.lang.python)
  • Re: How is the logical processing being done for strings like Dog and Cat
    ... All Python objects have a boolean context. ... Python's boolean operators are short-circuit operators. ... As a general rule, false values are empty: ...
    (comp.lang.python)
  • Re: Time we switched to unicode? (was Explanation of this Python language feature?)
    ... If had been an empty set, ... the same people that are defending the current choice now. ... Python didn't even have sets until 2.3, ... Python is, in a very real sense, built on dicts, not sets. ...
    (comp.lang.python)
  • Re: chop() and empty() functions
    ... empty, no-op function of some sort when you want to at least have ... If you intend to set explicit handlers later, wouldn't it be better for the default event handler to raise an exception, just in case your intention is waylaid or sidetracked? ... happening (although, this could currently be the case in Python, I'm not ... Python will not assume that you meant to supply a no-op function. ...
    (comp.lang.python)