0
animals = ["dog","cat","lion","elephant"]

animals[-1:-3]

What is wrong with this?

Gives me blank result

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 3
    You can't slice from right to left with a positive step (it defaults to `1`), did you want `[-3:-1]` (which would be `['cat', 'lion']`)? – jonrsharpe Oct 08 '20 at 18:23
  • syntax is `[start:stop]` under no circumstance start can be greater than stop. https://stackoverflow.com/questions/509211/understanding-slice-notation – Equinox Oct 08 '20 at 18:25
  • Thank You @venky__ animals[-3:-1] - This would have been my required syntax for the result. Cheers! – Sarad Jain Oct 08 '20 at 18:33
  • 1
    @venky__ the syntax is `[start:stop:step]`, so `start` can be greater than `stop` if `step < 0` – Pranav Hosangadi Oct 08 '20 at 20:27

0 Answers0