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
  matplotlib x axis range goes over the set range Pedroski55 5 527 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 753 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 616 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 848 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  Index data must be 1-dimensional : Classifier with sklearn Salma 0 1,923 Apr-01-2021, 03:22 PM
Last Post: Salma
  IndexError: list index out of range brunolelli 11 2,316 Mar-25-2021, 11:36 PM
Last Post: brunolelli
  IndexError: list index out of range ramu4651 2 1,387 Jan-24-2021, 01:45 PM
Last Post: buran
  string index out of range jade_kim 4 1,112 Jan-13-2021, 05:41 AM
Last Post: jade_kim
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 1,149 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Try/Exept prints only ones tester_V 11 1,448 Nov-03-2020, 02:38 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

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