checking if a list is empty

scattered tooscattered at gmail.com
Fri May 6 17:57:21 EDT 2011


On May 6, 2:36 am, 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:
> ...
>
> Thanks,
>
> Laszlo

is there any problem with

(3) if li == []:

?

Seems to work when I test it and seems to clearly test what you are
trying to test. The only problem might be if in some contexts == has
the semantics of checking for object identity.



More information about the Python-list mailing list