negative stride list slices

Julio Oña thinmanj at gmail.com
Wed Sep 1 18:31:09 EDT 2004


On Wed, 01 Sep 2004 18:05:22 -0400, Peter Hansen <peter at engcorp.com> wrote:
> 
> Uh, yeah.  Okay.  So let's say that part was obvious.  Now
> please explain *which* elements are being listed in reverse
> order, referring to the index value -3 and the elided
> index value.  Presumably one of those refers to the beginning
> or end of the list, while the other one refers to something
> that is referenced as "-3".  Can you point to a diagram or
> description in the documentation or tutorial which actually
> explains this?  Or can you (or anyone) explain it in your
> own words?  Or is everyone who thinks this makes sense just
> pretending to actually understand it?
> 

Sorry Peter to be to lassy.

Well in the case [:-3:-1] I think this is what is happening
  - revers the list 
  - this left with [0:2] (remember  [-3:] returns 2 characters only

in the case [-1:-2:-1]
  - revers the string 'Apleh'
  - this left with [:1] (this is tricky but -1 is equivalent to 0 as
-2 is to 1, remember you are counting backwards from the end)

The problem is the mening of -1 when reversing the list. The slice is
closed in the lower side and open in the upper one. When reversing the
list the lower end is open and no close as with positive step.

Any comments?


-- 
Julio



More information about the Python-list mailing list