What are negative indices? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What are negative indices?

In python language

30th Mar 2020, 2:57 AM
Vaibhav Nehe
Vaibhav Nehe - avatar
2 Answers
+ 1
Let’s take a list for this. >>> mylist=[0,1,2,3,4,5,6,7,8] A negative index, unlike a positive one, begins searching from the right. >>> mylist[-3] 6 This also helps with slicing from the back: >>> mylist[-6:-1] [3, 4, 5, 6, 7]
30th Mar 2020, 2:59 AM
Vaibhav Nehe
Vaibhav Nehe - avatar
0
Did you just answer to your self? and why is -3 equivalent to 6? shouldn't it start from 0?(just curious, I don't know python)
30th Mar 2020, 5:17 AM
xaralampis_
xaralampis_ - avatar