Why Python does *SLICING* the way it does??

Peter Hansen peter at engcorp.com
Wed Apr 20 20:10:44 EDT 2005


James Carroll wrote:
> If you have five elements, numbered 0,1,2,3,4  and you ask for the
> elements starting with the first one, and so on for the length you
> would have [0:length].  So [0:5] gives you elemets 0,1,2,3,4.   Think
> of the weirdess if you had to ask for [0:length-1] to get length
> elements...
[...]
> It is a little weired that slicing does [index: count] instead of
> [index:index] or [count:count] I agree, but python really does just
> flow wonderfully once you see how clean code is that's written [index:
> count].

I think you got confused part way through that.  Python's
slices are *not* index:count, but are index:index.  It's
just that for the example you gave, starting at 0, they
happen to amount to the same thing...

-Peter



More information about the Python-list mailing list