Re: checking if a list is empty

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

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

li = []

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

The Python core developers use the second form.
for the official recommendation.



Relevant Pages

  • Re: while c =
    ... 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. ...
  • 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: ...
  • 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: ...
  • 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. ...
  • 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. ...