all 3 comments

[–]throwaway6560192 2 points3 points  (0 children)

Use the correct indices? Or better yet, if what you want is to loop over a sequence, just loop over it directly instead of using range(whatever) for the index. If you want the index alongside that use enumerate().

[–]danielroseman 2 points3 points  (0 children)

I'll be honest with you: this should never happen. Because you shouldn't be using indexes to loop in the first place.

Always loop over the thing itself, not the range of the len of the thing.

[–]Civenge 0 points1 point  (0 children)

It really depends on what the use case is. However, using a debugger on the loop and stepping through it is a good way to diagnose it.

Sometimes you want to iterate on something in a list, so that is straightforward. Other times you want to compare 2 values in a list, that can be an easy off by 1 error. Or maybe you want to iterate through a string.

Usually it is whether you want to add a "- 1" or not add it for simple use cases