What is the logic behind programming languages 0-indexing?

585 views

As someone who primarily uses R, I don’t understand why Python indexes lists starting from 0. I’m very slow at simple mental calculations and doing something like subsetting an array in Python often takes me several extra seconds.

I think I read that it has something to do with memory, but thats so much less of a consideration for people who only use high level languages.

In: Technology

9 Answers

Anonymous 0 Comments

In 0-based indexing the index represents the **offset** relative to the start of the array.

It works best if you think of the underlying memory. Say you have an array that starts at address 200, and use an element size of 10. The first element (i=0) is *at* the start, so at address 200. The next one is at 210. With 0-based indexing the *i*th element is at 200 + *i*·10. In 1-based indexing the first element (i=1 here) is still at the start (200), so to make the formula work you need to subtract one when calculating the address: 200 + (*i*-1)·10.

1-based indexing may be useful when strictly dealing with ordinals (1st, 2nd, etc). But when you have to do arithmetic with your indices, 0-based is often easier and less error-prone.

You are viewing 1 out of 9 answers, click here to view all answers.