list.clear() missing?!?

Steven Bethard steven.bethard at gmail.com
Tue Apr 11 12:42:21 EDT 2006


Ville Vainio wrote:
> I tried to clear a list today (which I do rather rarely, considering
> that just doing l = [] works most of the time) and was shocked, SHOCKED
> to notice that there is no clear() method. Dicts have it, sets have it,
> why do lists have to be second class citizens?

This gets brought up all the time (search the archives for your 
favorite), but your options are basically (renaming your list to lst for 
readability) one of::

     del lst[:]

     lst[:] = []

or if you don't need to modify the list in place,

     lst = []

Personally, I tend to go Fredrik's route and use the first.

If you feel really strongly about this though, you might consider 
writing up a PEP.  It's been contentious enough that there's not much 
chance of getting a change without one.

STeVe



More information about the Python-list mailing list