The behavior of List.insert

Terry Reedy tjreedy at udel.edu
Tue Apr 29 12:57:54 EDT 2003


"York" <yorklee70 at yahoo.com> wrote in message
news:b8m8dn$b03$1 at pegasus.csx.cam.ac.uk...
> mylist = [1,2,3,4,5],
>
> mylist.insert(-1,99) will make mylist to be [99,1,2,3,4,5]
>
> just wonder why not let it to be [1,2,3,4,99,5]

Guido decided otherwise for whatever reason.  What you observed is
intended: from LibRef 2.2.6.4 Mutable Sequence Types
"(4)
When a negative index is passed as the first parameter to the insert()
method, the new element is prepended to the sequence. "
so this is not an implementation bug.

Terry J. Reedy








More information about the Python-list mailing list