Question about idioms for clearing a list

Fredrik Lundh fredrik at pythonware.com
Tue Feb 7 02:31:55 EST 2006


Steven D'Aprano wrote:

> > so we can have three ways to do the same thing?  the right way to
> > nuke a sequence is to do "del L[:]".  this is explained in Python 101.
>
> The Zen isn't "only one way to do it". If it were, we
> wouldn't need iterators, list comps or for loops,
> because they can all be handled with a while loop (at
> various costs of efficiency, clarity or obviousness).
>
> del L[:] works, but unless you are Dutch, it fails the
> obviousness test.

unless you read some documentation, that is.  del on sequences
and mappings is a pretty fundamental part of Python.  so are slicings.

both are things that you're likely to need and learn long before you
end up in situation where you need to be able to clear an aliased
sequence.

> It also fails the introspection test:

so does "print L".

</F>






More information about the Python-list mailing list