-2

How do I use the slice syntax of python to get a list from 0 to i in reversed order?

For example, if the list is [1, 2, 3, 4] and i == 2, the result should be [3 ,2 ,1].

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
muffel
  • 7,004
  • 8
  • 57
  • 98

1 Answers1

1

The 2 is the first character instead of the second because it takes effect after the reversal (you take steps backwards)

>>> [1,2,3,4][2::-1]
[3, 2, 1]
Vincent Savard
  • 34,979
  • 10
  • 68
  • 73