+ 1

Why in list (in python 3) to access the last element of a list, you use -1 and not -0?

anyone have any idea why python use -1 to access the last element of the list? why Python doesn't use -0?

15th May 2018, 6:39 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
4 Réponses
+ 8
Because -0 is equal to 0? So there is no movement or shifting beyond its current place, assuming that Python even lets you use -0. Think of it as an offset instead. -1 is equiv to starting at the 0 position and going backward by one place, which loops it around to the end of the list.
15th May 2018, 6:42 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 5
This way u can use a list like a circle: The left neighbour of 3 is 2 and the left neighbour of 0 is -1 its magic... right?😉
15th May 2018, 8:00 PM
Oma Falk
Oma Falk - avatar
+ 2
print(True) if -0 == 0 else print(False) https://code.sololearn.com/c4iJXaYP3rML/?ref=app
16th May 2018, 8:39 AM
Johannes
Johannes - avatar
0
Fata1 Err0r thanks!
24th May 2018, 9:34 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar