python for loop

Steven D'Aprano steven at
Wed Apr 1 10:06:28 CEST 2009

On Wed, 01 Apr 2009 04:39:26 +0100, Rhodri James wrote:

> Dragging this back to the original topic, you clearly find starting list
> indices from zero unintuitive.  To me, with a mathematical background,
> it's not just intuitive, it's correct.  All sorts of useful properties
> fall out from that, not the least of which is the fact that
> "a[0:len(a)]" slices the whole of a list.

But some non-useful properties fall out of that too.

Want the fifth item? a[5] gives you the *sixth* item, which is weird, so 
you have to use a[5-1] to get the fifth item.

There is a major clash between the names of ordinals in human languages 
and zero-based counting. In human languages, the Nth-ordinal item comes 
in position N. You can keep that useful convention with zero-based 
counting by inventing the ugly word "zeroth", but that just leads to 
bizarro-talk like "the zeroeth item comes first, the first item comes 
second, and so on".

a[0:len(a)] is legal, a[0] is legal, but surprisingly a[len(a)] is an 

Despite coming from a Pascal background, I've come to appreciate and 
prefer zero-based indexing for programming. But I'm not blind to the 
disadvantages. I'll often work out an algorithm using pencil and paper 
and counting from one, and then subtract one to get zero-based indexes.

There are advantages and disadvantages to both systems, but on balance, I 
think that zero-based is a better system for programming, and one-based 
for natural language.


More information about the Python-list mailing list