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
  Index data must be 1-dimensional : Classifier with sklearn Salma 0 347 Apr-01-2021, 03:22 PM
Last Post: Salma
  IndexError: list index out of range brunolelli 11 872 Mar-25-2021, 11:36 PM
Last Post: brunolelli
  IndexError: list index out of range ramu4651 2 525 Jan-24-2021, 01:45 PM
Last Post: buran
  string index out of range jade_kim 4 457 Jan-13-2021, 05:41 AM
Last Post: jade_kim
  Output prints at the end? LastStopDEVS 5 536 Dec-19-2020, 05:59 AM
Last Post: buran
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 629 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Try/Exept prints only ones tester_V 11 693 Nov-03-2020, 02:38 AM
Last Post: tester_V
  List index out of range error while accessing 2 lists in python K11 2 570 Sep-29-2020, 05:24 AM
Last Post: K11
  user inputing data until in range Omer_ 1 449 Sep-24-2020, 07:14 PM
Last Post: buran
  List index out of range error when attempting to make a basic shift code djwilson0495 4 792 Aug-16-2020, 08:56 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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