list.clear() missing?!?

Ville Vainio vivainio at gmail.com
Tue Apr 11 12:24:06 EDT 2006


Fredrik Lundh 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?
>
> because Python already has a perfectly valid way to clear a list,
> perhaps ?
>
>     del l[:]

Ok. That's pretty non-obvious but now that I've seen it I'll probably
remember it. I did a stupid "while l: l.pop()" loop myself.

> (lists are not mappings, so the duck typing argument don't really
> apply here.)

I was thinking of list as a "mutable collection", and clear() is
certainly a very natural operation for them.




More information about the Python-list mailing list