[Python-Dev] extended slicing for lists

Ka-Ping Yee ping@lfw.org
Sat, 29 Jul 2000 16:22:50 -0700 (PDT)


Michael Hudson wrote:
> Ka-Ping Yee <ping@lfw.org> writes:
> > [...] sufficient for me to suggest just dropping it:
> > better to avoid confusion altogether.
> 
> Well, the BDFL spoke.

Hence my mock-indignant "hmph". :)
 
> > having to say l[len(l)-1:-1:-1] would be a royal pain.
> 
> Well, you could probably always say 
> 
> l[sys.maxint:0:-1]
> 
> (your example returns the empty list!)

Hey, wait a second.  To get l[start:end:step], you start with 'start'
and keep adding 'step' until you get to -- but not including -- 'end',
right?  So l[len(l)-1:-1:-1] should return the entire list reversed,
not the empty list.

range() demonstrates the behaviour nicely:

    >>> size = 5
    >>> range(size-1,-1,-1)
    [4, 3, 2, 1, 0]

Wouldn't l[sys.maxint:0:-1] return all but the first element of l in
reverse order?

    >>> range(size-1,0,-1)
    [4, 3, 2, 1]

Note that we should deprecate the use of sys.maxint (preferring None)
in this situation, since if the step is less than -1, the elements
you get ought to depend on the value of sys.maxint % step.  I'd expect:

    >>> l = [0, 1, 2, 3, 4, 5]
    >>> l[5:0:-3]
    [5, 2]
    >>> l[6:0:-3]
    [4, 1]
    >>> l[7:0:-3]
    [3, 0]
    >>> l[8:0:-3]
    [5, 2]

hence

    >>> l[sys.maxint:0:-3]
    [???, ???]                     # depends on sys.maxint % 3

My guiding principle is that the indices should always match what
you get from range(), and if start or end are missing, you get the
items you would have gotten if you extend the sequence out forever.


-- ?!ng