checking if a list is empty

Raymond Hettinger python at rcn.com
Fri May 6 18:52:18 EDT 2011


On May 5, 11:36 pm, Jabba Laci <jabba.l... at gmail.com> 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




More information about the Python-list mailing list