Why we receive IndexError: list index out of range error in python ?

Sometimes we receive below error message while executing python script.

IndexError: list index out of range

Lets see how this error appears on the screen.

user@ngelinux:python$ ./sixth.py
first element
2
last
Traceback (most recent call last):
  File "./sixth.py", line 9, in 
    print(mylist[4])
IndexError: list index out of range
user@ngelinux:python$

As the name implies of the above error message, the list index which is called in the script does not exist.

Lets see the index called.

print(mylist[2])
### The above element in the array doesn't exists, i have corrected the index number which exists and it solved the issue

After correcting above issue, we can see now the issue is solved.

user@ngelinux:python$ ./sixth.py 
first element
2
last

first element
2
last
['first element', '2', 'last']
user@ngelinux:python$
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments