While counting the index from right side in a list, should we start from 0 or -1 ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

While counting the index from right side in a list, should we start from 0 or -1 ?

[::-2] which value is the [-2]? 3rd from the right or 2nd from the right ? I lost a challenge because of it. And in other challenge lost same question with other logic..

30th May 2020, 7:39 PM
Dhiraj Jaiswal
2 Answers
+ 1
Right side starts with -1. It's an abbreviation of length-1 which you would write in other languages.
30th May 2020, 7:44 PM
HonFu
HonFu - avatar
+ 1
Ah, okay, I see the issue now. The third slot of a slice is 'step'. The -2 has no direct relation to backwards indexing like array[-1]. -2 here just means that the slice makes downward jumps of 2.
30th May 2020, 8:32 PM
HonFu
HonFu - avatar