Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IndexError: list index out of range and python

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?

like image 528
Tyler Avatar asked Jul 08 '09 15:07

Tyler


People also ask

How do I fix error index out of range?

Solution using range() and len(): Subsequently preventing you from receiving the 'list index out of range' error in python. The range() function returns a sequence of numbers starting from 0, and ends at the integer passed as a parameter. We use this method along with the len() function.

What is index out of range error in Python?

The error “list index out of range” arises if you access invalid indices in your Python list. For example, if you try to access the list element with index 100 but your lists consist only of three elements, Python will throw an IndexError telling you that the list index is out of range.

How does Python handle IndexError?

The only way to avoid an IndexError in python is to make sure that we do not access an element from an index that is out of range. For example, if a list or a tuple has 10 elements, the elements will only be present at indices 0 to 9. So,we should never access the element at index 10 or more.

What does list index out of range mean?

Generally, list index out of range means means that you are providing an index for which a list element does not exist.


2 Answers

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.

like image 122
Alex Martelli Avatar answered Oct 04 '22 17:10

Alex Martelli


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?

like image 45
kjfletch Avatar answered Oct 04 '22 15:10

kjfletch