-4

I have below list

l1 = [1,2,3,4,5,6,7,8,9]

I want to understand below result :-

>>> l1
[4:8:-2]

Please explain result of above slicing.

inspectorG4dget
  • 104,525
  • 25
  • 135
  • 234
Ibney Hasan
  • 71
  • 1
  • 1
  • 3

1 Answers1

1

slicing is [start:stop:step] do like this, When you are steping it from backward then you have to take index from back,

>>> l1 = [1,2,3,4,5,6,7,8,9]
>>> l1[8:4:-2]
[9, 7]

You are asking to start from index 4 till index 8 by step -2, so you are asking to get index 8 and 6 and avoiding index 4.

Vishnu Upadhyay
  • 4,979
  • 1
  • 12
  • 26