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

Alan Gauld alan.gauld at btinternet.com
Thu Jul 26 10:04:05 CEST 2007

"Dick Moores" <rdm at rcblue.com> wrote

>>Not sure why it didn't help you Dick, but it led me to:
> Alan, I don't see an explanation of [::-1] anywhere in those 3 
> links.
> There needs to be a clear description and better examples somewhere
> in the docs, IMO.

I agree the explanation is 'terse' but it does explain that the syntax
is S[i:j:k] and that any/all of the values can be ommitted and what 
defaults are. It also explains the significance of -1 as a value.

So [::-1] is simply i, j  taking defaults and -1 for k

So its equivalent to S[0:len(S):-1]

But I agree there could be a few more examples of the use of
the k element.

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list