Python Forum
I hate "List index out of range"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I hate "List index out of range"
#3
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
Reply


Messages In This Thread
I hate "List index out of range" - by Melen - May-09-2023, 09:44 PM
RE: I hate "List index out of range" - by bowlofred - May-10-2023, 02:56 AM
RE: I hate "List index out of range" - by Melen - May-10-2023, 09:34 PM
RE: I hate "List index out of range" - by Melen - May-10-2023, 09:40 PM
RE: I hate "List index out of range" - by ibreeden - May-11-2023, 09:38 AM
RE: I hate "List index out of range" - by perfringo - May-11-2023, 01:10 PM
RE: I hate "List index out of range" - by DeaD_EyE - May-11-2023, 03:58 PM
RE: I hate "List index out of range" - by Melen - May-11-2023, 09:09 PM
RE: I hate "List index out of range" - by Melen - May-12-2023, 05:17 AM
RE: I hate "List index out of range" - by Melen - May-12-2023, 08:42 PM
RE: I hate "List index out of range" - by Melen - May-13-2023, 11:17 AM
RE: I hate "List index out of range" - by Melen - May-13-2023, 01:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 7,160 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 2,120 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,584 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,557 May-03-2022, 01:39 PM
Last Post: Anldra12
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,855 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,437 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 3,027 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,496 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 2,310 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  IndexError: list index out of range brunolelli 11 6,937 Mar-25-2021, 11:36 PM
Last Post: brunolelli

Forum Jump:

User Panel Messages

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