[Tutor] s.insert(i, x) explanation in docs for Python 3.4 confusing to me

Ben Finney ben+python at benfinney.id.au
Sat Jan 16 18:58:15 EST 2016


boB Stepp <robertvstepp at gmail.com> writes:

> So in this model of understanding negative list indexing, should it be:
>
> mylist = [ 100, 200, 300, 400, 500 ]
>           ^    ^    ^    ^    ^   ^
>           -5   -4   -3   -2   -1  ?

For completeness, let's use the rest of the integers also::

              0    1    2    3    4    5
              ↓    ↓    ↓    ↓    ↓    ↓
    mylist = [ 100, 200, 300, 400, 500  ]
              ↑    ↑    ↑    ↑    ↑    ↑
             −5   −4   −3   −2   −1    ?

> But in this model, what should go in the place of "?"?

You can use ‘len(mylist)’ for the index at the end of the sequence.

There isn't a negative number which will address that position; it isn't
needed, because there is already one obvious way :-)

-- 
 \            “Without cultural sanction, most or all of our religious |
  `\          beliefs and rituals would fall into the domain of mental |
_o__)                                 disturbance.” —John F. Schumaker |
Ben Finney



More information about the Tutor mailing list