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
#1
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?

Thanks,
Michael

PS - I am happy to give you the code and any other information that might be helpful
Reply
#2
>>> 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

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Down I hate "List index out of range" Melen 20 3,304 May-14-2023, 06:43 AM
Last Post: deanhystad
  zfill prints extra et the end of a var tester_V 4 894 Mar-24-2023, 06:59 PM
Last Post: tester_V
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 6,308 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,899 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,316 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,435 May-03-2022, 01:39 PM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,175 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,834 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,301 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  variable prints without being declared. ClockPillow 2 1,803 Jul-11-2021, 12:13 AM
Last Post: ClockPillow

Forum Jump:

User Panel Messages

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