What way is the best to check an empty list?
Martin P. Hellwig
martin.hellwig at dcuktec.org
Wed Mar 25 17:26:10 EDT 2009
Raymond Hettinger wrote:
> On Mar 25, 7:38 am, srinivasan srinivas <sri_anna... at yahoo.co.in>
> wrote:
>> For ex: to check list 'A' is empty or not..
>> if A == []:
>> if A.count == 0:
>> if len(A) == 0:
>> if not A:
>
> PEP 8 recommends the latter.
>
>
> Raymond
I can't seem to find where this recommendation is mentioned or implied.
Personally I would go for the len version for the sole reason that its
immediate clear what it is what I am trying to do here. So that when I
reread my code a couple of weeks later I don't have to think, eh was A a
bool, oh no I was checking if that list had content in it.
But that could be just me though :-)
YMMV
--
mph
More information about the Python-list
mailing list