Python Forum
I hate "List index out of range" - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: I hate "List index out of range" (/thread-39949.html)

Pages: 1 2 3


I hate "List index out of range" - Melen - May-09-2023

Hello!

I have a question, but please don't give me the answer. I'm just looking for some tips. I'm new to Python, and I'm posting here because I can't understand the logic of the "index out of range" error. I know that in Python we count from 0, but I'm (almost) sure that I haven't made any mistakes, and it still doesn't work... Wall

Is there something I should know? I'm hesitant to post my program's code because I really want to figure it out on my own, but I need some help.


I would have liked to figure it out on my own without having to ask, but oh well...


RE: I hate "List index out of range" - deanhystad - May-10-2023

It could be many different errors you could make that result in an index out of range error, but they mostly fall into a few categories: You are not adding items to the list when you think you are. You are not indexing the list you think you are. Your counting is off.


RE: I hate "List index out of range" - bowlofred - May-10-2023

Besides the error, the traceback should also show you the variable that is causing the problem and the line number.

You can add some print statements on the line before to check what you are assuming. Let's say that I get the error like this:

Error:
Traceback (most recent call last): File "/tmp/indexerr.py", line 4, in <module> info = mylist[x] ~~~~~~^^^ IndexError: list index out of range
I might add some statements on line 3 that give me more information:

print(f"mylist has a length of {len(mylist)} and x has a value of {x}")
Now I can run the program and get more info:
Output:
mylist has a length of 15 and x has a value of 30 ...
Oh, no wonder I got an index error. Now I can track down why x is 30 here instead of whatever I expected it to be.


RE: I hate "List index out of range" - Melen - May-10-2023

(May-09-2023, 09:44 PM)Melen Wrote: Hello!

I have a question, but please don't give me the answer. I'm just looking for some tips. I'm new to Python, and I'm posting here because I can't understand the logic of the "index out of range" error. I know that in Python we count from 0, but I'm (almost) sure that I haven't made any mistakes, and it still doesn't work... Wall

Is there something I should know? I'm hesitant to post my program's code because I really want to figure it out on my own, but I need some help.


I would have liked to figure it out on my own without having to ask, but oh well...

Thank you for your answer, I looked at all that but it does not seem to me to be that.


RE: I hate "List index out of range" - Melen - May-10-2023

Ok I guess I don't know how to respond properly to messages besides that!

deanhystad: Thanks for your answer, I looked and normally it shouldn't be one of those errors

bowlofred: Thank you!! I was doing this type of check before but not as accurate with a sentence like this comparing multiple variables. I test in all directions as I can and I come back to you :)


RE: I hate "List index out of range" - ibreeden - May-11-2023

A common mistake is to change a list over which one is iterating. Are you perhaps doing something like this?
mylist = ["one", "two", "three", "four"]
for i in range(len(mylist)):
    removed_item = mylist.pop(i)
    print(f"Removed item {removed_item}")
Output:
Removed item one Removed item three
Error:
Traceback (most recent call last): File "/home/ibreeden/PycharmProjects/Forum/forum02.py", line 3, in <module> removed_item = mylist.pop(i) IndexError: pop index out of range



RE: I hate "List index out of range" - Gribouillis - May-11-2023

Melen Wrote:I hate "List index out of range"
Python's error tracebacks are the most useful things. They always tell you where the error comes from if you take the time to read them. In the glorious days of the C language, the error message in such cases was the infamous
Error:
Segmentation fault: core dumped
without any indication of what happened. You could spend weeks searching the mistake in your code.


RE: I hate "List index out of range" - perfringo - May-11-2023

(May-09-2023, 09:44 PM)Melen Wrote: I can't understand the logic of the "index out of range" error

What specifically is beyond your understanding? If you count all items in object starting from zero and get 0, 1, 2 then what should happen if you are looking for item 4? To put it another way - if you have deck of 5 cards what should you say (or do) if somebody asks to give 6th card in the deck?

Slices can be out of range without raising error:

>>> s = 'abc'
>>> len(s)
3
>>> s[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> s[3:]
''
>>> s[100:120]
''
>>> l = [1, 2, 3]
>>> l[3:]
[]



RE: I hate "List index out of range" - deanhystad - May-11-2023

I think this discussion would be more fruitful if you would post your code. That would provide us with some insight about your blind spots.


RE: I hate "List index out of range" - DeaD_EyE - May-11-2023

Indexing starts with 0.

The element at index 0 is the first
and at index -1 is the last element.

     
the list      :  [ "a", "b", "c", "d" ]
positive index:  [  0    1    2    3  ]
negative index:  [ -4   -3   -2   -1  ]
"a" is at index 0 and index -4
"b" is at index 1 and index -3
...

Accessing the list with an index, which does not exist, then it raises the IndexError.
This is also true for negative index which is out of range.