clear derived list

John Hunter jdhunter at ace.bsd.uchicago.edu
Tue Mar 11 18:08:26 EST 2003


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





More information about the Python-list mailing list