checking if a list is empty

Gregory Ewing greg.ewing at canterbury.ac.nz
Wed May 11 19:08:04 EDT 2011


Hans Georg Schaathun wrote:
>  0 is a number as real and existent as any other,
> one would think that the empty list is also as real and existent as
> any other list.

0 does have some special properties, though, such as
being the additive identity and not having a multiplicative
inverse. Adding falseness as another special property isn't
too much of a stretch and turns out to be useful.

Likewise, it's useful to treat empty containers as having
a similar special property, since they're often a base or
terminating case in algorithms.

It's especially useful in a dynamic language where any
additional operations such as finding the length or
comparing with zero has a run-time cost.

Yes, you have to learn it, but it's a small thing to
learn with a considerable payoff.

-- 
Greg



More information about the Python-list mailing list