D–Data
D.index
A D.index error occurs when an invalid index is used when walking through an array or other data structure.
Many languages use zero-based arrays; that is, the valid indices for an array of size n go from 0 to n-1. This leads to a common indexing error when looping through such an array, starting at 1 instead of 0. (In the Python example here, note that range(1,n) includes numbers from 1 to n-1.)
for i in range(1, n): # code that processes array[i]
Beginning the indexing at 1 instead of 0 causes the code to miss the first element of the array. (In Python, you can write range(n) as a shortcut for range(0,n), which makes this error less likely to occur.) Similarly, you can make the same mistake on the other end, going past the end ...
Get Find the Bug A Book of Incorrect Programs now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.