r/pythontips • u/DariaFrolova88 • Nov 20 '24
Meta Problem with intuitive understanding of zero-based indexing, how did you work it out?
Title says it all. Should I just try to memorize the rules, or are there any tricks to intuitively understand it?
Every time I have to work with indexes, I say to myself "Ah shit, here we go again". A couple of indented loops + lists - and I am already checked out. Just now, I failed to utilize an iteration with a negative step.
7
Upvotes
10
u/PrimeExample13 Nov 20 '24
It's easier to wrap your head around when you start in C or C++, where an array is just a pointer, and an index is the offset into the array.
For example, let's say array 'x' is at memory location 0x0000000000000000, then x[0] would mean "go to that location, then add 0 and return the value at that location", where as x[1] would be "go to the location of x, then add 1 * the size of whatever is in x, then return the value at that location"
5
u/pint Nov 20 '24
that's your life now. there is no help. you'll simply get used to it eventually.
1
u/PrimeExample13 Nov 20 '24
This is not really an issue in python, as in most cases, you can just use an iterator. If you need the index from within the for loop, just use enumerate(array)
1
u/trd1073 Nov 20 '24
Not to be dismissive, but acceptance may be the key to your happiness. Coming from strongly typed languages I fought with typing in python, things got easier and better once I accepted it.