1

Can someone please explain how does [::-1] work? I have read somewhere that it reverses the list, I tried it and it actually works, but I do not know how. What do the 2 colons mean? I could not find anything like this in python documentations.

Also, does someone know the efficiency of this or the underlying algorithm?

vaultah
  • 40,483
  • 12
  • 109
  • 137
Zoli
  • 122
  • 9

1 Answers1

8

It's the slice notation:

[start:stop:step]
  • start is the index where you start. If it's omitted, Python assumes you want to start at the beginning.
  • stop is where you want to stop. If you omit it, Python assumes you want to go until the end.
  • step is what the -1 is taking advantage of. 1 is the default value. 2 iterates over every other element. -1 iterates over all of the elements, but backwards.
Blender
  • 275,078
  • 51
  • 420
  • 480