Hello,I'm Francis, a beginner programmer from Ghana, West Africa.
I was wondering why the list .index() method doesn't return negative indices as well as well as positive indices.
Although ambiguity will make it difficult to implement, in certain cases, it would be useful if it could return the negative index.
For instance, if one creates an if statement that checks whether an element is the last item in a list as follows:
listy = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
if listy.index(10) == -1:
I understand that the same effect can be achieved with the index notation - as in
if listy[-1] == 10:
but the way that came naturally to me was to use the .index method
rather than index notation, and it took a very long time for me to
figure out why my code was not working(mostly because I'm a beginner).
So do what you will, I guess.:)