-3

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?)

Luke
  • 2,751
  • 1
  • 17
  • 20
  • 3
    [*"Martin Richards, creator of the BCPL language (a precursor of C), designed arrays initiating at 0 as the natural position to start accessing the array contents in the language, since the value of a pointer p used as an address accesses the position p + 0 in memory."*](https://en.wikipedia.org/wiki/Zero-based_numbering) – Robert Harvey Jan 28 '20 at 03:46
  • Yes, -0 will introduce even more problems and complications. Because by definition, integer and long don't have -0. – Sraw Jan 28 '20 at 03:46
  • A fun and interesting post relating to this question: [How to inherit and extend a list object in Python?](https://stackoverflow.com/questions/4093029/how-to-inherit-and-extend-a-list-object-in-python) The author asks: "I am interested in using the python list object, but with slightly altered functionality. In particular, I would like the list to be 1-indexed instead of 0-indexed." (Please don't actually do this -- just for fun sake.) – felipe Jan 28 '20 at 04:24

1 Answers1

1

Even if we've moved past C's reasoning for arrays starting at 0 (i.e. array indices were direct abstractions for memory address offsets), starting array/list indexing at 0 still simplifies a lot of different index-related math stuff. Here's a Quora post on the subject. These things are much more common use cases than reverse-list-indexing, so it would make sense from a design perspective that they would be given priority.

As for negative list indices, there really wasn't much other option. After all, indices have to be integers, and while 0 and -0 might be meaningfully different in the floating-point standard, they evaluate to the same integer value and are thus indistinguishable. We need to use something instead, and -1 makes the most sense.

Certain programming languages are 1-indexed, including (but not limited to) MATLAB, Mathematica, Lua, APL, and even grandaddy FORTRAN. You'll note a theme here in what these languages are designed to do - none of them are geared towards generalized software development or computer tasks. Rather, they're biased towards mathematical tasks, towards use cases and functions where 1-indexing is more convenient or makes more sense. Python has different priorities.


If you need to wrap your head around the abstraction, you can consider how we would do rear-indexing in other languages - say, Java:

boolean isPalindrome(List x) {
    for(int i = 0; i < x.size() / 2; i++)
        if(x.get(i) != x.get(x.size() - i - 1))
            return false;
    return true;
}

Note that the i - 1 is present here too - it's the exact same indexing idea, except python makes it slightly more convenient by no longer requiring us to explicitly find the length of the list.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53