Re: checking if a list is empty
- From: Raymond Hettinger <python@xxxxxxx>
- Date: Fri, 6 May 2011 15:52:18 -0700 (PDT)
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: 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) - Re: Boolean tests [was Re: Attack a sacred Python Cow]
... patronizing me ... You suggested a syntax for testing non-emptiness (`x is not empty`) ... some idiot did just that almost immediately. ... Python, you are simply wrong. ... (comp.lang.python) |
|