<div class="gmail_quote">On Wed, Jan 6, 2010 at 8:54 AM, spir <span dir="ltr">&lt;<a href="mailto:denis.spir@free.fr">denis.spir@free.fr</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

Hello,<br>
<br>
Just found something rather misleading with negative indices:<br>
<br>
s = [1,2,3,4,5,6,7,8,9]<br>
print s, s[-3], s[-4]   # [1, 2, 3, 4, 5, 6, 7, 8, 9] 7 6<br>
s.insert(-3, 0)<br>
print s, s[-3], s[-4]   # [1, 2, 3, 4, 5, 6, 0, 7, 8, 9] 7 0<br>
<br>
So, I did insert 0 at index -3, but s[-3] is still 7, &amp; 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&#39;s a bit disturbing. What do you think?<br>


(And: should insert behave so that 0 actually is at index -3, meaning insert it after 7?)<br>
<br>
(No issue with positive indices, indeed.)<br></blockquote><div><br></div><div>I think it may just be an issue with misunderstanding what the insert does:</div><div><br></div><div>&gt;&gt;&gt; help(list.insert)</div><div>

<br></div><div>insert(...)</div></div><div>    L.insert(index, object) -- insert object before index</div><div><br></div><div>When you insert(-3, 0) you are inserting 0 before index -3. When it says &quot;before&quot; it has nothing to do with the direction of travel.</div>

<div><br></div><div>And when you say there&#39;s no issue with positive indices, take a look at this case:</div><div><br></div><div><div>In [6]: s = [1,2,3]</div><div><br></div><div>In [7]: s[0]</div><div>Out[7]: 1</div>
<div>
<br></div><div>In [8]: s.insert(0,0)</div><div><br></div><div>In [9]: s[0]</div><div>Out[9]: 0</div><div><br></div></div>(ignoring mathematical considerations about the sign of 0)<div><br></div><div>You get a different value at the same position.</div>

<div><br></div><div>It seems that your confusion lies with this statement: <br clear="all">&quot;insertion adds an index&quot;</div><div><br></div><div>which it doesn&#39;t - insertion adds an object to your list. It&#39;s a fundamental difference - the computer stores objects, and if we want to access that object we have to know where it&#39;s stored. In your first case, 7 and 6 are stored in the positions 3 and 4 from the end of the list. When you insert(-3, 0) you&#39;re shifting everything before -3 and putting 0 in the spot before (at least that&#39;s how I think of it. How python actually implements lists is probably different.).</div>

<div><br></div><div>Does that make sense? (and if I&#39;ve explained it incorrectly, feel free to correct me!)</div><div>HTH,</div><div>Wayne</div><div>-- <br>To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi<br>


</div>