[Tutor] list.insert(<negative index>)

spir denis.spir at free.fr
Wed Jan 6 15:54:14 CET 2010


Hello,

Just found something rather misleading with negative indices:

s = [1,2,3,4,5,6,7,8,9]
print s, s[-3], s[-4]	# [1, 2, 3, 4, 5, 6, 7, 8, 9] 7 6
s.insert(-3, 0)
print s, s[-3], s[-4]	# [1, 2, 3, 4, 5, 6, 0, 7, 8, 9] 7 0

So, I did insert 0 at index -3, but s[-3] is still 7, & 0 is in fact at index -4. Well, this can be explained: insertion adds an index, there are now 10, so when counting backwards a given index does not point to the same position/item anymore than before insertion. Still, it's a bit disturbing. What do you think?
(And: should insert behave so that 0 actually is at index -3, meaning insert it after 7?)

(No issue with positive indices, indeed.) 

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/


More information about the Tutor mailing list