-3

I'm trying to reverse a numpy array, and for some reason only this method seemed to work. What does ::-1 do exactly?

  • Have you researched slicing in the documentation? (This also works on Python lists.) – S3DEV May 21 '22 at 20:56
  • In Python, you can extract an element from an array (or character from a string) using the index of the item. You can also use square brackets to get a slice of the array/string. The first number indicates the starting point, the second number indicates the end point (this last index is not included in the result), the final number indicates the step size. A step size of 1 includes every item, 2 excludes every other item etc. A negative value counts backwards from the end. Hence ```::-1``` has no start/end point (so uses whole array), whilst -1 indicates it should count from the end backwards – Ben May 21 '22 at 20:57
  • Welcome to Stack Overflow. "I'm trying to reverse a numpy array, and for some reason only this method seemed to work." I assume that you did not type the code purely by chance, and discover that it happened to do exactly what you wanted. You would have to have *looked it up* somewhere, right? So - did you try going back to the place where you found that example, and *reading the surrounding text*? – Karl Knechtel May 21 '22 at 21:09

0 Answers0