The behavior of List.insert

Alex Martelli aleax at aleax.it
Wed Apr 30 00:00:59 CEST 2003


York wrote:

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

Agreed -- and indeed, in 2.3 beta 1...:

[alex at lancelot src]$ python
Python 2.3b1 (#1, Apr 26 2003, 10:44:25)
[GCC 3.2 (Mandrake Linux 9.0 3.2-1mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x=list('ciao')
>>> x.insert(-1, 'w')
>>> x
['c', 'i', 'a', 'w', 'o']
>>>

...the issue is finally fixed.


Alex





More information about the Python-list mailing list