5 ms·
It's the difference between numbering the contents of the list (1 indexing), and measuring the distance from the beginning of the list to the start of the item
by sammorrowdrums 8y ago
It's the difference between numbering the contents of the list (1 indexing), and measuring the distance from the beginning of the list to the start of the item (0 indexing).
Personally I'm happy to switch between both, and they both have positives and negatives when I actually write code.
0 indexing is not incorrect, or incompatible with maths, it is just a different way of conceiving of lists/arrays by considering the index to more like the distance from the beginning. The first item begins 0 spaces from the start position.
That is why in that context array length is usually the highest index + 1, because length is the distance from the start position to the end of the list, whereas the index is the start position of the item in the list.
I will concede that 0 indexing is a little too close to the storage paradigm for arrays (i.e. a sequential list of items of n bytes) and that when so often these arrays point to more complex data-structures / objects, the distance doesn't make as much sense as the item number... but meh - I'd take Python generators / iterators at the expense of 0 indexing any day.
- coldtea 8y ago>It's the difference between numbering the contents of the list (1 indexing), and measuring the distance from the beginning of the list to the start of the item (0 indexing). The problem is that one is indeed an indexing (numbering the contents 1...X...N and asking for item X, customers[X]), whereas the other is not, but is used as an indexing (e.g. customers[5] is not getting the 5th item but the sixth).
- Twisol 8y agoWhere does your definition of "an indexing" originate? The word "index" literally means "to point at" (hence, index/pointer finger), and in this sense, every element may indeed be indexed -- in this case, by means of a unique integer. If I had to give a name to the concept you're talking about, it would be an "ordinal index".
- coldtea 8y ago>If I had to give a name to the concept you're talking about, it would be an "ordinal index". Which is both the naive understanding of a numeric index in everyday life in general, and the most common case in mathematics (and most math/scientific software).
- e12e 8y agoIndex finger, or first finger, right next to the zeroeth finger, the thumb... ;)
- vvillena 8y agoIn 1-based arrays the number is an index, but in 0-based arrays it is an offset. Languages like C or Rust make this behavior very explicit.