-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
  • 107,083
  • 22
  • 201
  • 376
muffel
  • 6,359
  • 5
  • 51
  • 86

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
  • 32,695
  • 10
  • 65
  • 72