[Python-Dev] Negative index in insert()
Eric S. Raymond
Fri, 24 Aug 2001 19:51:56 -0400
>>> a = [1, 2, 3, 4]
I expected that a.insert(-2, 0) would yield [1, 2, 0, 3, 4]. It was a
rude shock to discover that
[0, 1, 2, 3, 4]
In fact I think this may be the nastiest surprise Python has handed me since
I started using it.
The reference manual says "same as s[i:i] = [x] if i >= 0" which of course
doesn't cover the i < 0 case. David Beasley's reference says "Inserts x
at index i" which sounds like the behavior I was expecting but didn't get.
Is this a deliberate design choice, an oversight, or a plain bug? If it's
a choice, it's damn poorly documented -- this deserves at least a footnote
in the list methods table. If it's an oversight or bug, I volunteer to fix it.
<a href="http://www.tuxedo.org/~esr/">Eric S. Raymond</a>
"America is at that awkward stage. It's too late to work within the system,
but too early to shoot the bastards."
-- Claire Wolfe