clear derived list

sismex01 at hebmex.com sismex01 at hebmex.com
Wed Mar 12 00:07:19 CET 2003


> From: John Hunter [mailto:jdhunter at ace.bsd.uchicago.edu]
> Sent: Tuesday, March 11, 2003 5:08 PM
> 
> I have a class derived from a list.  I want to be able to set the list
> from another list, first clearing the existing members.  I don't see
> an analog to the clear method of dictionaries.
> 
> Here is what I am doing now
> 
> class mylist(list):
>     def set(self, seq):
>         # flush the old list
>         [self.pop() for i in range(len(self))]
>         self.extend(seq)
> 
> l = mylist()
> l.append(1)
> l.append(2)
> 
> x = [3,4,5]
> l.set(x)
> print l
> 
> This prints [3,4,5], which I want.  But my gut tells me there is a
> better way.
> 
> John Hunter
>

Ugh.  You want something like this:

> class mylist(list):
>     def set(self, seq):
>         self[:] = seq

To delete all items of a list:

self[:] = []

-gustavo





More information about the Python-list mailing list