list.clear() missing?!?

Peter Hansen peter at engcorp.com
Thu Apr 13 07:48:18 EDT 2006


Duncan Booth wrote:
> Peter Hansen wrote:
> Looking in the 'obvious' place in the Tutorial, section 5.1 'More on
> Lists' I found in the immediately following section 5.2 'The del 
> statement': 
> 
>>There is a way to remove an item from a list given its index instead
>>of its value: the del statement. Unlike the pop()) method which
>>returns a value, the del keyword is a statement and can also be used
>>to remove slices from a list (which we did earlier by assignment of an
>>empty list to the slice). 

I saw that section too, but was scanning for any example of wiping out 
the whole list.  As you point out, it's not mentioned.  I don't think 
there's even an example of slicing with no arguments [:] for copying a 
list (i.e. on the right side of the =), and it's reasonable to assume (I 
originally did, as I recall) that this would be some kind of error...

> Both of these talk about ways to remove slices from a list. Perhaps the 
> wording could be clearer to make it obvious that they can also be used to 
> clear a list entirely (using the word 'clear' would certainly help people 
> Googling for the answer). So maybe 'this can even change the size of the 
> list or clear it completely' would be a good change for 3.1.4.

This is quite true.  After all, who imagines when offered a "slice of 
cake" that a slice might be the entire thing!  The concept of "slice" in 
English strongly implies a *subset*, not the whole, so if we're not 
going to get a .clear() method, I do believe that the various uses of 
[:] should be much more explicitly pointed out in the docs.  At least 
we'd have a ready place to point to in the tutorial, instead of this 
discussion cropping up every month.

-Peter




More information about the Python-list mailing list