list.clear() missing?!?

Duncan Booth duncan.booth at invalid.invalid
Thu Apr 13 03:40:34 EDT 2006


Peter Hansen wrote:

>> * learning slices is basic to the language (this lesson shouldn't be
>> skipped)
> 
> And yet it doesn't appear to be in the tutorial.  I could have missed 
> it, but I've looked in a number of the obvious places, without
> actually going through it (again) from start to finish.  Also,
> googling for "slice site:docs.python.org", you have to go to the
> *sixth* entry before you can find the first mention of "del x[:]" and
> what it does.  I think given the current docs it's possible to learn
> all kinds of things about slicing and still not make the non-intuitive
> leap that "del x[slice]" is actually how you spell "delete contents of
> list in-place". 

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). 

The 'earlier showing assignment of an empty list to a slice' is a reference 
to section 3.1.4 'Lists': 

> Assignment to slices is also possible, and this can even change the
> size of the list: 
> 
> 
>>>> # Replace some items:
> ... a[0:2] = [1, 12]
>>>> a
> [1, 12, 123, 1234]
>>>> # Remove some:
> ... a[0:2] = []
>>>> a
> [123, 1234]
> 

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.



More information about the Python-list mailing list