-3

I know that python list starts its index from the value of 0 to n. So like the first element of the list l is l[0] etc.

This is super trivial to think of. However, I find myself struggling to find the correct index when things get messy and complicated. Due to the fact, I have to compensate for the length by +/- 1 every time I want to access an element.

This look like a simple problem. But it is not. Is there a systematic way I can deal with this? Is there a way to look at this problem from a different point of view?

petezurich
  • 9,280
  • 9
  • 43
  • 57
  • Going through that struggle will help you a lot than you can think of. – Rahul Jun 02 '19 at 13:28
  • If it's easy for you to grasp the idea that indexes start from zero, I'm not sure why it is confusing that you need to access the `n-1`th index if you want the `n`th element. – DeepSpace Jun 02 '19 at 13:28
  • Possible duplicate of [Why does the indexing start with zero in 'C'?](https://stackoverflow.com/questions/7320686/why-does-the-indexing-start-with-zero-in-c) – joel Jun 02 '19 at 13:29
  • 1
    @JoelBerkeley It's not a duplicate. OP does not ask *why* the indexes start from 0 – DeepSpace Jun 02 '19 at 13:30
  • @DeepSpace Knowing why might help understand how to think of it. It _can_ answer "Is there a systematic way I can deal with this?". Perhaps it's not quite a duplicate, but I that answer might provide this answer too – joel Jun 02 '19 at 13:31
  • 1
    Often you can use the `for x in mylist:` syntax (for collections implementing `__iter__`) combined with `enumerate()` to get the indices automatically - so unless I need to work the indices, I try to avoid using them. Depends on the use-case though. `range` also helps as it's endstop is exclusive, so you can do `for i in range(len(mylist)):`. Wrapping your head around thinking zero-indexed is just a matter of experience, it will become natural. – Jeppe Jun 02 '19 at 13:33
  • If you need to access a thing in a list by its index, maybe you should store it in a dictionary instead and associate with a key that makes more sense than an arbitrary index. – wwii Jun 02 '19 at 13:35
  • also see https://softwareengineering.stackexchange.com/q/110804/271736 – joel Jun 02 '19 at 13:39

2 Answers2

3

When I teach the concept of element indexing to novice programmers, a metaphor I use is to think of the indexes and numbers in a ruler. And of the contents in the list (or array, vector, string, etc...), as physical objects that are placed on this ruler - say a pebble or anything. You place the objects between the the numbers - not on their midle. So the first peble is in the space between the 0 and 1 markers. The second is the space between the 1 and 2. And the last one will be on the space between the 29 and 30 markers, even if the ruler is 30cm long - after the 30, the rule is over, and no other elements can be placed.

That not only helps visualize how elements start at index 0, but also with Python slicing notation, in which the end of the slice denotes the limit up to the elements are picked (and not the last element to be picked).

So, on a list, lst[0:1] will pick all elements between the 0 and 1 markers, and lst[0:28] will pick the elements between those numbers, and the last two elements, between [28:30] are left out.

(on an unrelated note, I hope you do not actually use l as a variable name - one letter variable names are usually not a good practice, with good sense prevailing, but l specifically is hard to distinguish from 1, so it makes the code harder to read)

jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • I like your perspective. However, for the comment for `l` and `1` I think you are correct, however in the context of programming, anyway a variable with name `1` is not permitted. I get your point, however. – Rafael Jun 02 '19 at 14:02
2

I am not sure exactly how you are looking at it. But this is how I first grasped this concept: In python, as well as in majority of other programming languages, the array positions are more like a distance from the beginning. It really helps if you start your programming learning with C, in which arrays are stored in consecutive memory blocks. Therefore, if I have an int array of length 5, the language will assign a 10 byte block (by default int in C is 2 byte). Now, the first int is stored in the first 2-byte slot of the block, i.e. right at the beginning. Hence the distance of that item from beginning is 0, which is the index of the item in the array. The next int is stored in bytes 3-4, i.e. the 2nd slot of the block. Now distance of this slot from the beginning is 1 slot, hence the index is 1, and so on.

Hope this way of looking into the intricacies of the language helps you. But I will be able to help you more if you give me one use-case where you are facing the challenge with indexing.

Neel
  • 360
  • 1
  • 10