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

boB Stepp robertvstepp at gmail.com
Sat Jan 16 19:43:57 EST 2016


Alex sent me this off-list.  I hope he does not mind me sharing part
of what he wrote on-list!

On Sat, Jan 16, 2016 at 4:57 PM, Alex Kleider <akleider at sonic.net> wrote:
> On 2016-01-16 14:39, boB Stepp wrote:
>
>
>>>>> mylist[:0 or None]
>>
>> [100, 200, 300, 400, 500]
>>
>> The critical portion of the for loop for me to understand, since it
>> results in [100, 200, 300, 400, 500] instead of the empty list.  But
>> what the heck is going on here?

[...]

> I guess when used in slices, None represents '-0'; clever really.
> A way to distinguish -0 from 0 which in simple arithmetic are one and the
> same.

I have no clue whether Alex's hypothesis is what actually goes on in
the implementation details, but things sure act like he is correct.
This led me to try:

>>> mylist[:None]
[100, 200, 300, 400, 500]

So, in effect, None is acting as a place holder for that final
position in slices.  Also, I would never have thought to be able to
use a logical "or" inside an index in Peter's "[:-i or None]".


-- 
boB


More information about the Tutor mailing list