Posts: 8
Threads: 1
Joined: May 2023
May-09-2023, 09:44 PM
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...
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...
Posts: 6,783
Threads: 20
Joined: Feb 2020
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.
Posts: 1,583
Threads: 3
Joined: Mar 2020
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.
Gribouillis likes this post
Posts: 8
Threads: 1
Joined: May 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...
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.
Posts: 8
Threads: 1
Joined: May 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 :)
Posts: 582
Threads: 1
Joined: Aug 2019
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
Posts: 4,786
Threads: 76
Joined: Jan 2018
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.
Posts: 1,950
Threads: 8
Joined: Jun 2018
May-11-2023, 01:10 PM
(This post was last modified: May-11-2023, 01:10 PM by perfringo.)
(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:]
[]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 6,783
Threads: 20
Joined: Feb 2020
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.
Posts: 2,125
Threads: 11
Joined: May 2017
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.
|