[Edu-sig] Confusion with slice notation

Douglas S. Blank dblank at brynmawr.edu
Mon Jan 5 13:14:29 EST 2004

Trent A. Oliphant wrote:
 > Here is where I get caught each time with list notation.
 > (I realized as I was writing this it isn't the slice notation but the 
 > notation that gets me)

I don't think it is the slice or the index notation that confuses many, 
but the fact that the negative numbers are distances to a line outside 
the array, and positive numbers are distances to the first element in 
the array. Or, negative numbers are distances, and positive numbers are 

Here is your example again, in a slightly different form. First column, 
positions from the start. Second column is distances from the end. Then 
the values from the x and y arrays:

      x y
------------- end/top
8 -1 9 1
7 -2 8 2
6 -3 7 3
5 -4 6 4
4 -5 5 5
3 -6 4 6
2 -7 3 7
1 -8 2 8
0 -9 1 9 ---- start/bottom

Why did they do it this way? Why not in a more symmetric way?

If they made it so that they were both zero-based, then x[-0] would be 
the last element. But -0 == 0, so that doesn't work.

If they made it so that they were both one-based, then lots of old C 
coders would have off-by-one errors :) Seriously though, if you think of 
the positive number as being a distance from the start of the array, 
then you can just take the address of the array and add the position to 
it (times the size of an element), and that will give you the address of 
that element. That explanation seems outdated, though.

But that is why they are asymmetric.


> x[:3] returns a list with the first three elements in it.
> x[-3:] returns a list with the last three elements in it.
> x[2] returns the third element in a list, but
> x[-3] returns the third to the last element in a list
> To illustrate it another way
> x = [1,2,3,4,5,6,7,8,9]
> y = [9,8,7,6,5,4,3,2,1]
> The way I look at it (and this may be where the problem lies) is that the
> [-n] notation is an inverse of sorts of the [n] notation.
> Slicing does work that way:
> x[:3] = [1,2,3] (3 elements)
> y[-3:] = [3,2,1] (3 elements)
> However, indexing doesn't
> x[3] = 4  (0 based indexing)
> y[-3] = 3 (could be viewed as -1 based indexing)
> So, I can't take my model for the slices and translate it to indexing, even
> though the notation is very similar.
> It just seems a bit inconsistent to me, and as I said earlier, I haven't
> been able to get it.  I am not very good at just memorizing something and
> saying that's just the way it is, I need more, and I haven't figured out
> what that more is for this issue.
> Just some food for thought.
> Trent Oliphant
> _______________________________________________
> Edu-sig mailing list
> Edu-sig at python.org
> http://mail.python.org/mailman/listinfo/edu-sig

Douglas S. Blank,         Assistant Professor
dblank at brynmawr.edu,            (610)526-6501
Bryn Mawr College,   Computer Science Program
101 North Merion Ave,       Park Science Bld.
Bryn Mawr, PA 19010  dangermouse.brynmawr.edu

More information about the Edu-sig mailing list