list.insert question

Brian Kelley bkelley at wi.mit.edu
Wed Jan 8 16:33:50 EST 2003


So I was playing with list.insert and expected

l=[1,2,3,4]
l.insert(-2, 5)

to become
l = [1,2,3,5,4]

This is mainly because the slice "wraps" (nicely) around to the end of 
the list.

I checked the manaul and indeed:

http://python.org/doc/current/lib/typesseq-mutable.html#l2h-153

(4) When a negative index is passed as the first parameter to the 
insert() method, the new element is prepended to the sequence.

I was wondering why this was the case.  Is there really a dichotomy 
between slices and inserts?  Is this something that will trip up a 
novice or is this geared toward 
messing-up-people-like-me-expecting-too-muchly-yours?

Brian.






More information about the Python-list mailing list