List Index out of Range Python for Loop
The List is generally used in Python to store multiple items or elements inside one single variable. The List is ordered in nature, the List can contain the elements inside them as arbitrary objects, and the elements inside the List can be accessed using the indexing starting from 0 to length-1; the List can be even nested to the arbitrary depth and also the List data Structure is mutable and are dynamic.
How to Create a List in Python?
The List is one of the unique features, or let’s say it is one of the unique Data Structures in Python. It can be declared inside the square braces [ ] with a relevant variable name depicting the data domain inside the List. There is no restriction on the data type of the List as the elements can be of any data type even each element inside the List of them can be of a different data type too.
See code below as an example:
Code
wild_animals = ['Lion', 'Tiger', 'Cheetah', 'Jaguar', 'Panther', 'Bear']
print(wild_animals)
Output
['Lion', 'Tiger', 'Cheetah', 'Jaguar', 'Panther', 'Bear']
How to Check the Length of a List in Python?
To check the length of a List in Python, we need the List to be created and then elements inside it and by using the print function in which we pass the length function, which is written as len() and inside the len() function braces we pass the variable name of the List that we have created and it prints the length of the List that we have created. Length in a List is nothing, but it tells us the number of elements present inside the List.
See code below as an example:
Code
wild_animals = ['Lion', 'Tiger', 'Cheetah', 'Jaguar', 'Panther', 'Bear']
print(len(wild_animals))
Output
6
Accessing the Elements inside the List in Python
In the above cases, we have seen how to print all the elements inside the List using the print function at once, but we will now see how to access a particular element inside the List using the indexing concept in List Python.
See codes below as an example
Code 1:
wild_animals = ['Lion', 'Tiger', 'Cheetah', 'Jaguar', 'Panther', 'Bear']
print(wild_animals[4])
Output
Panther
Code 2:
wild_animals = ['Lion', 'Tiger', 'Cheetah', 'Jaguar', 'Panther', 'Bear']
print(wild_animals[5])
Output
Bear
We have seen in the above cases how to access the elements inside the List using the indexing concept, and by using the print function in Python, we will now try to access the elements inside the List using the iterative method, that is by using one of the iterating conditions which ids for loop in this case
See the code below, for example, to understand accessing the elements using for loop in Python
Code
wild_animals = ['Lion', 'Tiger', 'Cheetah', 'Jaguar', 'Panther', 'Bear']
for i in wild_animals:
print(i)
Output
Lion
Tiger
Cheetah
Jaguar
Panther
Bear
Understanding the List Index out of Range error in Python
When trying to access the elements inside the List using the indexing, we know that if the length of a particular List is about 6, then the maximum index that we can pass as a parameter is five because the indexing starts from 0 and goes on till length-1. If we try to access the elements more than five, the compiler throws a List Index out of range error to correctly pass the index parameter to access the elements inside the List.
See code below as an example to understand List Index error
Code
wild_animals = ['Lion', 'Tiger', 'Cheetah', 'Jaguar', 'Panther', 'Bear']
print(wild_animals[7])
Output
Traceback (most recent call last):
File "<string>", line 2, in <module>
IndexError: list index out of range
Understanding the List index error in Python For Loop
Similarly, even in iterators here for a loop, when we pass the wrong value in the range function braces while using the for loop, the Python Compiler throws a List Index out of Range error in Python for Loop. Passing the wrong integer or indexing reference in the braces can cause this error.
See the code below to understand the List Index out-of-range error in Python for the loop.
Code
wild_animals = ['Lion', 'Tiger', 'Cheetah', 'Jaguar', 'Panther', 'Bear']
for wild_animal in range(7):
print(wild_animals[wild_animal])
Output
Lion
Tiger
Cheetah
Jaguar
Panther
Bear
Traceback (most recent call last):
File "<string>", line 3, in <module>
IndexError: list index out of range