Python "why" questions

Roald de Vries downaold at gmail.com
Sat Aug 7 12:03:05 CEST 2010


On Aug 7, 2010, at 5:46 AM, Vito 'ZeD' De Tullio wrote:
> Default User wrote:
>
>>> From "the emperor's new clothes" department:
>>
>> 1)  Why do Python lists start with element [0], instead of element  
>> [1]?
>> "Common sense" would seem to suggest that lists should start with  
>> [1].
>
> http://userweb.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

I think the reason why is just historical; C uses zero-based indices.  
In C, an array index is an offset with respect to the pointer that the  
array variable actually is, so 0 makes sense (my_array[0] == *my_array).

I'm not convinceed (yet) by Dijkstra's reasoning. *Maybe* if you want  
to describe a range with two </<='s, it makes sense. But Python (nor  
C, nor ...) uses that notation. I agree with the OP that the first  
item in a list would most naturally be called item 1, and therefore  
have index 1. (This doesn't mean I'm in favor of 1-based indices)

One of the reasons I like python so much, is that you (almost) never  
have to use indices. Normally you just iterate over the elements. If I  
ever need indices, it's a strong indication that I actually want a  
dictionary.

Cheers, Roald





More information about the Python-list mailing list