Question about idioms for clearing a list

Raymond Hettinger python at rcn.com
Tue Feb 7 10:08:17 EST 2006


> I'm a fairly average programmer (better than average compared to my
> immediate colleagues).  I've read every tutorial I can get my hands
> on, but I have no _memory_  of ever coming across the del keyword, let
> alone that it is fundamental to Python,

My thought is that get/set/del are fundamental ops in almost any
computing context:  songs on your iPod playlist, records in a database,
files in a directory, or elements in a Python list.

Individual element deletion doesn't seem to come-up as much in Python
because we tend to build data collections and then throw them away in
their entirity.  However, the concept becomes more prominent when
dealing with persistence or when mutating a collection in-place.  The
OP was specifically seeking to do in-place modifications and so found
himself in need of understanding the del-statement.  IMHO, it goes with
the territory.



> Similarly, I remember slices simply because they are handy, not
> because I have ever heard of them being fundamental before.

FWIW, I regard slices to be fundamental to the language not because
they are easy or that everyone automatically knows about them; rather,
I am just recognizing that the concept of slices pervades the language
and comes up in many different guises.  It is a lesson that should not
be skipped. That is why I think that the OP's problem is better solved
by improved documentation than by adding an unnecessary method to the
list API.



> As a general rule of thumb, I would say that if a person is struggling
> with a language, it is primarily a problem with the language, and than
> problem with the documentation, and lastly a problem with the person.

I would swap the first two.  No language has yet been invented
(including HTML and LOGO) that someone doesn't struggle with.  In
contrast, no documentation has ever been written that couldn't be
improved.

The "problem with the person" part goes last only because it suggests
immutability.  The reality is that incomplete learning is a fixable
problem (independent of language or doc issues).  If I never learn the
del-statement, should I be surprised that I stuggle with how to express
deletion?



Raymond




More information about the Python-list mailing list