The behavior of List.insert

York yorklee70 at yahoo.com
Tue Apr 29 19:19:47 CEST 2003


>> 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.

Thanks. I don't know what Guido's reason is, but I still believe that it is 
nice to allow list.insert distinguish different negative indice rather than 
treat all them as zero.

Cheers,

York




More information about the Python-list mailing list