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

boB Stepp robertvstepp at gmail.com
Sat Jan 16 00:05:35 EST 2016


On Fri, Jan 15, 2016 at 10:53 PM, Cameron Simpson <cs at zip.com.au> wrote:
> On 15Jan2016 22:20, boB Stepp <robertvstepp at gmail.com> wrote:

>> I always get an empty list, which is actually what I was expecting, so
>> I do not see how s[i:i] can ever equal [x].
>
>
> It isn't an equality test (==), it is an assignent. It is saying "set the
> zero length sequence at index i to the one element sequence [x]".

Ah!  That makes sense.


>>>>> things.insert(-1, 'What the heck?!?')
>>>>> things
>>
>> [0, 'Hmm...', 3, 'WhackABunny', 6, 'What the heck?!?', '?']
>>
>> "...at the index..." to me would mean that 'What the heck?!?' should
>> become the last item in the list.  Again, the interpreter help gave
>> what I was expecting.
>
>
> To me it means "insert 'x' so that its index is 'i'".

But that's my point!  In my example x (here 'What the heck?!?') is
*not* at index i (here, -1).  Instead it winds up at index -2.  But
this fits in perfectly with the interpreter help, since it winds up
*before* index i (-1).

-- 
boB


More information about the Tutor mailing list