Python Forum
index of range, but data prints out
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
index of range, but data prints out
I have written a program in python. It occasionally gives an index out of range at a particular line in the code. I printed out all of the variables referenced in the offending line and they all printed out fine. I am thoroughly confused. How can all the indexed variables print out in one line and then raise an error in the next.

Can you offer any help in debugging this problem?


PS - I am happy to give you the code and any other information that might be helpful
>>> spam = [1, 2] # name spam refers to list
>>> spam
[1, 2]
>>> spam[2] # max index is 1, so this will raise error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> spam
[1, 2]
Basically your variable is some container type that can be access by index, like list, tuple, etc. But you try to access element by index that does not exists (i.e. it has less elements than you think). Note in python indexes start at 0.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: list index out of range dolac 4 422 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 638 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 597 May-03-2022, 01:39 PM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 1,387 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 1,455 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 1,121 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 1,462 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  Index data must be 1-dimensional : Classifier with sklearn Salma 0 3,113 Apr-01-2021, 03:22 PM
Last Post: Salma
  IndexError: list index out of range brunolelli 11 3,619 Mar-25-2021, 11:36 PM
Last Post: brunolelli
  IndexError: list index out of range ramu4651 2 2,175 Jan-24-2021, 01:45 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020