checking if a list is empty

Adam Tauno Williams awilliam at whitemice.org
Fri May 6 16:05:09 EDT 2011


On Fri, 2011-05-06 at 14:49 -0500, harrismh777 wrote:
> Terry Reedy wrote:
> >>> (2) if not li:
> >> This is fine.
> > This is the intended way. Anything in addition is extra noise and wasted
> > calculation. In other words, let Python do the boilerplate work for you.
>     I agree, but I don't like it.

+1  This is the Python reality-distortion-field at work.

Motto#1: Python is all about readability!
Motto#2: Crytic code is awesome if it is Pythoncally cryptic!

I'd never accept code like "if not x" as an empty test.

> ... if not li  says nothing about what li is supposed to 'be' and 
> implies in any case that li does not exist, or worse is some kind of 
> boolean.
> li is in fact an empty list [] and will identify as such, and of 
> course (as a list object) has all of the attributes and methods of a list...
> Why not have a list method that makes this more explicit:
> if not li.emptylist()
> if not li.empty([])
> there might be others...

Good luck.  Just code -
  # You can immediately tell what this is meant to do!
  if len(x) == 0:  
- and ignore the Pythonistas [they're nuts;  that x.count() doesn't work
is amazingly stupid].




More information about the Python-list mailing list