Like in most languages, the first item in a Python list (or string) is indexed at 0. But does this design make sense given that one can access the last item in a list (or string) with the value -1. For example:
my_list = list("Madam Im Adam")
print(my_list[0])
# prints M
print(my_list[-1])
# prints m
Given that Python allows programmers to access the end of a list using negative values wouldn't it make sense to have broken with convention and designed Python so that list indexes started at 1? Examine for example this simple algorithm to identify palindromes:
phrase = "madam im adam"
for i in range(0, len(phrase ) // 2):
if phrase [i] != phrase [-i - 1]: #awkward!!!! (why not just -i?)
is_palindrome = False
if is_palindrome:
print("It's a palindrome")
else:
print("It's NOT a palindrome")
If list and string indexes started at 1 instead of 0 the above algorithm with this awkward line:
if phrase [i] != phrase [-i - 1]:
could simply be written as:
if phrase [i] != phrase [-i]:
Which is much simpler to read and comprehend. So is this a design flaw in Python?
(Guido van Rossum probably had reasons to start list indices at 0. If he wanted to retain that orthodox functionality, but also make a language where palindrome algorithms were simple to write would it have made any sense for him to specify that the last item in a list be accessed with the value -0 instead of -1? Or does -0 in this context introduce even more problems and complications?)