Possible improvement to slice opperations.

Ron Adam rrr at ronadam.com
Mon Sep 5 12:23:46 EDT 2005


Szabolcs Nagy wrote:

> with the current syntax L[i:i+1] returns [L[i]], with nxlist it returns
> L[i+1] if i<0.
> 
> L=range(10)
> L[1:2]==[L[1]]==[1]
> L[-2:-1]==[L[-2]]==[8]
> 
> L=nxlist(range(10))
> L[1:2]==[L[1]]==[1]
> L[-2:-1]==[L[-1]]==[9] # not [L[-2]]
> 
> IMHO in this case current list slicing is more consistent.


Your second case should be:
    L[-3:-2]==[L[-2]]==[8]

The single index is also the right side index of the range selection 
ending in the same position just as with positive number the single 
index is the left side index of a range starting in the same position.

So for positive indexing...
    L[n1:n2]==[L[n1]]==first value of selection from left

and negative indexing...
    L[n1:n2]==[L[n2]]==first value of selection from right


Currently for negative values...

    L[n1:n2]==[L[n2-1]]==first value of selection from right


The symmetry is easier to see when using the '~' values.

LL=list(range(10))
Lx=nxlist(range(10))

LL[ 1: 2]==[LL[ 1]]==[1]
Lx[~2:~1]==[Lx[~1]]==[8]


Cheers,
Ron




More information about the Python-list mailing list