IndexError: list index out of range and python


Question

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?

1
17
10/3/2017 2:09:27 AM

Accepted Answer

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

36
7/8/2009 3:07:15 PM

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?


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon