Does "IndexError: list index out of range" when trying to access the N'th item mean that my list has less than N items?
Asked Answered
K

7

54

I'm telling my program to print out line 53 of an output. Is this error telling me that there aren't that many lines and therefore can not print it out?

Kirwin answered 8/7, 2009 at 15:3 Comment(0)
S
87

If you have a list with 53 items, the last one is thelist[52] because indexing starts at 0.


From Real Python: Understanding the Python Traceback - IndexError:

IndexError

The IndexError is raised when you attempt to retrieve an index from a sequence, like a list or a tuple, and the index isn’t found in the sequence. The Python documentation defines when this exception is raised:

Raised when a sequence subscript is out of range. (Source)

Here’s an example that raises the IndexError:

test = list(range(53))
test[53]

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-6-7879607f7f36> in <module>
      1 test = list(range(53))
----> 2 test[53]

IndexError: list index out of range

The error message line for an IndexError doesn’t give you great information. You can see that you have a sequence reference that is out of range and what the type of the sequence is, a list in this case. That information, combined with the rest of the traceback, is usually enough to help you quickly identify how to fix the issue.

Suwannee answered 8/7, 2009 at 15:7 Comment(0)
P
36

Yes,

You are trying to access an element of the list that does not exist.

MyList = ["item1", "item2"]
print MyList[0] # Will work
print MyList[1] # Will Work
print MyList[2] # Will crash.

Have you got an off-by-one error?

Procreant answered 8/7, 2009 at 15:8 Comment(0)
R
8

The way Python indexing works is that it starts at 0, so the first number of your list would be [0]. You would have to print[52], as the starting index is 0 and therefore line 53 is [52].

Subtract 1 from the value and you should be fine. :)

Ramadan answered 3/5, 2017 at 8:58 Comment(0)
F
5

Yes. The sequence doesn't have the 54th item.

Futtock answered 8/7, 2009 at 15:7 Comment(0)
K
4

That's right. 'list index out of range' most likely means you are referring to n-th element of the list, while the length of the list is smaller than n.

Kaylakayle answered 8/7, 2009 at 15:8 Comment(0)
L
2

Always keep in mind when you want to overcome this error, the default value of indexing and range starts from 0, so if total items is 100 then l[99] and range(99) will give you access up to the last element.

whenever you get this type of error please cross check with items that comes between/middle in range, and insure that their index is not last if you get output then you have made perfect error that mentioned above.

Lietuva answered 13/6, 2016 at 8:34 Comment(0)
O
2

If you read a list from text file, you may get the last empty line as a list element. You can get rid of it like this:

list.pop()
for i in list:
   i[12]=....
Oldie answered 26/4, 2019 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.