[Tutor] What exactly is [::-1]?

Luke Paireepinart rabidpoobear at gmail.com
Thu Jul 26 05:38:37 CEST 2007


cuell wrote:
> In order to reverse the order of an array, I discovered that I'm 
> supposed to use [::-1]. 
>   
I don't know if 'supposed to' is the correct term.
You could just as easily get away with using ['a','b','c'].reverse().
However, below you're using 'array' and I'm not sure exacly what this is.
Does it have to do with the built-in array  module or one of the 
numeric/numpy variants?
>  >>> a = array([1., 2., 3.])
>  >>> a
> array([ 1.,  2.,  3.])
>  >>> a[::-1]
> array([ 3.,  2.,  1.])
>  >>>
>
> I would like to know what exactly the index notation of [::-1] is, where 
> it comes from and if there are other variants.
>   
This is called list slicing.  Look into it to figure out what all this 
stuff means.
I could send you a link but I'd just google 'python list slicing' to 
find it, so I'll leave that as an exercise for the reader.
> Thank you for your help.  I lurk about on this list and have learned a 
> fair bit.
>   
Good to hear :)
-Luke



More information about the Tutor mailing list