[Edu-sig] indexing, slicing (was : the state of the link)

Kirby Urner urnerk at qwest.net
Wed Apr 21 12:44:56 EDT 2004


Hi Denis --

Part of why I like Python slicing conventions is that mylist[0] is
non-problematic, i.e. if mylist[1] were to return the first element, as in
some other languages I use (e.g. xBase), then what would mylist[0] return?
None?  Empty set?  An out of range exception?  

My feeling is that if you using indexing at all (as indicated by [x]) then
you should be expecting some element in the set, not no element at all.

Note that it is possible to force an empty list using slice notation, simply
by giving x:x as the range.  That's to squeeze the range to nothing, as the
first element we want is also the first one we don't want -- nothing in
between.

Since it's convenient to have negative indexing start us from the end of the
list, it would be a glaring hole to have:

mystuff[-2] mystuff[-1] (mystuff[0] <-- null) mystuff[1] mystuff[2]... etc.

Instead, when we go negative, we immediately begin from the other end, which
is intuitive (change in sign = change in starting position).  The offset
from the start of the list is now 1 in the other direction.

Also, I do not think the concept of "open set" should apply, given indexing
is confined to the integers.  The slice [2:10] is very definitely closed
i.e. there's no neighborhood of convergence as in [2:10), where you have the
real or rational numbers to move you as close to 10 as you like (without
ever reaching it).  

Slice notation is all about closed sets, even though the 2nd index
represents the first one you don't want to include.

Also, in your post, you often say "to" as in 2-to-10.  Then you argue that
this intuitively should include the 10th element.  To my mind, this argues
for *not* translating the slice operator : as 'to'.  

Maybe it would be better to say 'slice 2, 10'.  For example, ordinary
language users wouldn't normally say 'negative 2 to 3' either, when thinking
of elements -2, -1, 0, 1 2, i.e.  we just wouldn't spontaneously think of -2
or -1 as being from the end of the list, period.  

So the whole idea of 'to' is really only a partial analogy, and, I'm
suggesting, a somewhat misleading one.

You could think of 0-based indexing as a vertical bar with pegs, and bags
(elements) hanging from the pegs.  The index is how far from the top of the
bar a peg is, with 0 being the very top:

 | 0
 |  \
 | 
 | 1
 |  \
 |
 | 2
 |  \

The range 0:3 means I want the bags at 0,1,2 whereas the 3 denotes the first
peg I want to ignore (a closed set).  There doesn't even have to be a peg 3
for mystuff[0:3] to make sense, e.g.:

  >>> mystuff = ['pig','cow','farmer']  # <-- mystuff[3] is out of range
  >>> mystuff[0:3]
  ['pig', 'cow', 'farmer']

However, I have no fear of when I range from -3 to 2:

 >>> for i in range(-3,3):
	  print mystuff[i],

	
 pig cow farmer pig cow farmer

i.e. mystuff[0] does not stand out as anomalous, between "ordinary" indexes
of -1 and 1.  Rather, all indexes in this range point to elements.  I find
this much easier to work with.

Kirby






More information about the Edu-sig mailing list