
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: print("Monty Python") I understand that the same effect can be achieved with the index notation - as in if listy[-1] == 10: print("Monty Python") - 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.:)

index is meant to return the index of the first match it finds in the list from the beginning, it's most simpless implementation is: ``` for i, v in enumerate(self): if v == value: return i throw ValueError(f"Value '{value}' is not in list") ```` if you want the negative index, just subtract the list's length from the returned index: ``` res = listy.index(10) - len(listy) ```

index is meant to return the index of the first match it finds in the list from the beginning, it's most simpless implementation is: ``` for i, v in enumerate(self): if v == value: return i throw ValueError(f"Value '{value}' is not in list") ```` if you want the negative index, just subtract the list's length from the returned index: ``` res = listy.index(10) - len(listy) ```
participants (4)
-
Evpok Padding
-
Francis O'Hara Aidoo
-
Greg Ewing
-
William Pickard