negative stride list slices

Shalabh Chaturvedi shalabh at cafepy.com
Wed Sep 1 23:16:10 CEST 2004


Reid Nichol wrote:
> This was the part that I was refering to:
> 
>  +---+---+---+---+---+
>  | H | e | l | p | A |
>  +---+---+---+---+---+
>  0   1   2   3   4   5
> -5  -4  -3  -2  -1
> 
> 
> Does it not all work the same in practice?


I'd like to add:

   +---+---+---+---+---+
   | H | e | l | p | A |
   +---+---+---+---+---+
   0   1   2   3   4   5
  -5  -4  -3  -2  -1
   |                   |
  start               end   (defaults for +ve step)
  end                 start (defaults for -ve step)

(Is this correct?)

David Abrahams wrote:
 > Can anyone explain the logic behind the behavior of list slicing with
 > negative strides?  For example:
 >
 >   >>> print range(10)[:-3:-1]
 >   [9,8]
 >
 > I found this result very surprising

What were you expecting?

Thanks,
Shalabh




More information about the Python-list mailing list