0-based indices

Why Python uses zero-based indices and not one-based as in Matlab, Mathematica, and R?

Why computer scientists count from zero?

(At) lunch break

Antje Jones: I had a list of letters:

>>> letter=['A','B','C','D','E']

and tried to get the second letter 'B':

>>> letter[2]


Surprisingly I got a 'C'?! This is confusing! A is the 0th letter?! No one counts like that!

Mike Zero: I agree, for working with objects, 0-based makes no sense. But starting at zero is the way of thinking about time (and data-streams). We don't see the index as an label of an object (for this we use dictionaries), we see the index as a start position of a new interval: a new hour or a new block of data (reading the next 100 letters from a file). We don't 'count' from zero, we simply mean the 'first' element starts at 0.

Let's look to my morning live-stream from midnight (zero) until now:


| | | | | | | | | | | | | |

0 1 2 3 4 5 6 7 8 o'clock 9 10 11 12 now(end)

What I did between 8 and 10 o'clock?

>>> at[8:10]

['breakfast', 'goToWork']

..and then? What I did from 10 until lunch

>>> at[10:12]

['programming', 'meeting']

Lunch was at 12

>>> at[12]


When is the meeting?

>>> at.index('meeting')


ah, OK at 11 o'clock

Antje Jones: OK, I understand, 0-based (starting with 0) is useful when working with a time-like sequence of data. But I need to work with objects: biological samples (1st, 2nd, 3rd, etc.) and 20,000 genes for which starting with 1 is more intuitive: gene[1], gene[2],...

Mike Zero: Unfortunately, it's not possible or at least very confusing to have both 0-based and 1-based indices in the same programming language. Therefore, mathematical languages working with objects (samples) are often 1-based. More general-purpose languages (C++, Java, and Python) typically used to read and process data-streams, are 0-based.

In Python, you can avoid using direct indices in for loops by using in

for element in mylist:


Also, some commands offer a switch to 1-based indices

for i, element in enumerate(letter,1):


(1, 'A')

(2, 'B')

And, instead of using lists, you can handle objects by using dictionaries.

Antje Jones: Thanks a lot!

Persons and dialogue are invented, inspired from discussions at