List Assignment Index out of Range in Python
As we know, a List is one of the four unique data structures available in Python; In this tutorial, we will deep dive into understanding iterating through a list and many more.
What is a List in Python?
A list can be defined as the ordered collection of mutable elements, which can be of any data type declared inside the [..] separated by commas after each element. Lists are indexed, so the elements inside the List can be accessed anytime through indexing the List.
Create a List in Python
The List is usually declared with a variable name suitable to the data it contains and with square braces where each of the items which are defined in it is separated by commas. The information the List contains can be of any data type with no restriction on that part.
See the code below to understand the method of creating lists in Python
Code
Speaking_Languages = [‘Hindi’, ‘English’, ‘Urdu’, ‘French’, ‘Telugu’, ‘Tamil’, ‘Kanada’]
print(Speaking_Languages)
Output
[‘Hindi’, ‘English’, ‘Urdu’, ‘French’, ‘Telugu’, ‘Tamil’, ‘Kanada’]
Accessing Elements through Indexing from the List
The elements inside the List we created above can be accessed using different methods. There are some pre-defined functions, looping through the List to access all elements, accessing a particular element, accessing elements using the variable and passing the index of the element that we want to print.
See the code below for reference to see how the elements can be accessed.
Code
Speaking_Languages= [‘Hindi’, ‘English’, ‘Urdu’, ‘French’, ‘Telugu’, ‘Tamil’, ‘Kanada’]
print(Speaking_Languages[1])
print(Speaking_Languages[3])
Output
[‘English’]
[‘French’]
Now that we have seen the way to access the elements inside a list in Python, we will now quickly move on to see the List Assignment Index out of Range in Python. Still, before that, we will understand what list assignment and what is index out of range in Python programming language.
What is List Assignment Index out of Range in Python?
In the above example, we have seen that while we are trying to access the elements inside the List using the print function and then passing the variable name inside it and then passing the index of the element that we would like to print as an argument to the variable name as a prompt we have got the result we require to be printed as an output in our respective console. But what happens if we try to pass an index which is out of the range of elements we have declared inside the List or in the case where the user input let us say about five elements then the minimum prompt that we can pass as an index is four because the indexing in the List or in majority cases but if we give index five or more then the program when executed throws an error saying ‘List Indexing Error’ which explaining List Index out of range.
See the below code, for example, for list index assignment out of range.
Code
Speaking_Languages= ['Hindi', 'English', 'Urdu', 'French', 'Telugu', 'Tamil', 'Kanada']
print(Speaking_Languages[7])
Output
Traceback (most recent call last):
File "C:\Users\PycharmProjects\pythonPrac\main.py", line 2, in <module>
print(Speaking_Languages[7])
IndexError: list index out of range
Similarly, if we try passing the index from -1 as a prompt inside the square braces, then we expect that it will return the same list index out of range error. Still, it prints the last element of that particular List instead it is because it is one of the ways to print the elements at the last element and respectively from -1, -2, -3 and so on till the last element index here in the above example till -7.
See the below code, for example.
Code
Speaking_Languages= ['Hindi', 'English', 'Urdu', 'French', 'Telugu', 'Tamil', 'Kanada']
print(Speaking_Languages[-1])
Output
Kanada
In the previous example, we have seen that when the indexing method, whether it is starting from 0 or -1, if the indexing goes out only, then the List assignment index out of range occurs similarly when the indexing goes out for negative indexing reference even then the error occurs.
See the below code for an example.
Code
Speaking_Languages= ['Hindi', 'English', 'Urdu', 'French', 'Telugu', 'Tamil', 'Kanada']
print(Speaking_Languages[-8])
Output
Traceback (most recent call last):
File "C:\Users\PycharmProjects\pythonPrac\main.py", line 2, in <module>
print(Speaking_Languages[7])
IndexError: list index out of range