Adding item in front of a list

Skip Montanaro skip at
Wed Apr 9 17:16:53 CEST 2003

    Thomas> insert(0, 1) behaves like insert(-1, 1). It is better, but
    Thomas> insert(1) would be best. 

.insert() is more general than what you want.  It can insert elements
anywhere in the list.  You are asking for something like .prepend().  For
that, just subclass list and add that method:

    class mylist(list):
        def prepend(self, obj):
            return self.insert(0, obj)

I hope you realize that the signature of .insert() can't be changed, beauty
or not.  *Way* too much code would have to be changed.


More information about the Python-list mailing list