The TypeError: ‘NoneType’ object is not subscriptable exception is raised if you try to subscript any object with a value of None. The term “subscript” refers to the process of retrieving values through indexing.
The purpose of this tutorial is to explain what the TypeError NoneType object is not subscriptable error means and how to resolve this TypeError in your program using real-world scenarios.
When TypeError: ‘NoneType’ Object Is Not Subscriptable Occurs
Subscriptable objects are objects that implement the __getitem__ method in Python, and they are objects that can be subscripted. Objects that can be subscripted include list items, dictionaries, and tuples, to name a few. With the help of Indexing, we can retrieve the items from these objects.
When you assign the result of built-in methods like append(), sort(), and reverse() to a variable, you will receive the TypeError: ‘NoneType’ object is not subscriptable error, which is the most common exception in Python.
In the case of assigning these methods to variables, they return the value None. Let’s look at an example to see if we can replicate the problem.
Let us see in the below example code when you get the TypeError: ‘NoneType’ Object Is Not Subscriptable and how you can fix it.
#Initializing a integer listNum = [5,2,3,4,1] #Sorting The Above List sortedList = listNum.sort() #Printing the Sorted List print("The Sorted List is: ", sortedList) #Print The First Element Of Sorted List print(sortedList)
The Sorted List is: None Traceback (most recent call last): File "c:\Users\ravi9\Coduber\test.py", line 11, in <module> print(sortedList) TypeError: 'NoneType' object is not subscriptable
You can see that we started with a list of random numbers and then tried to sort the list using the built-in sort() method, which we then assigned to an output variable in the above example.
As a result, when we print the output variable, we receive the value None. In the following step, we attempt to access the element by indexing it, believing it to be of type list, and we receive the following TypeError: The ‘NoneType’ object does not have a subscriptable property.
You will receive the same error if you attempt to perform any other operations on subscriptable objects such as lists, dictionaries, and tuples, such as append(), reverse(), and so on. It serves as a design principle for all mutable data structures in the Python programming language.
Note: Python does not allow you to subscript integer objects; if you do, Python will raise a TypeError: Integer objects cannot be subscripted. The ‘int’ object cannot be subscripted.
How To Fix TypeError: ‘NoneType’ Object Is Not Subscriptable Occurs
Now that you’ve grasped the concept, we encounter a TypeError when we attempt to index the None Value. The issues will be addressed in a variety of ways.
Due to the fact that the sort() method returned a value of None, and we were assigning that value to an output variable and indexing it, our code above was throwing a TypeError.
Most effectively, this problem can be avoided by not including the sort() method in any variables and simply leaving the numbers as they are.
Let us see in the below code example how you can fix the above code and get the sorted list and also access the first element of the list without any issues.
#Initializing a integer listNum = [5,2,3,4,1] #Sorting The Above List listNum.sort() sortedList = listNum #Printing the Sorted List print("The Sorted List is: ", sortedList) #Print The First Element Of Sorted List print(sortedList)
The Sorted List is: [1, 2, 3, 4, 5] 1
Although the list is being sorted, no variables are being assigned as a result. Then I assigned the list to a new variable to store the sorted list, and I access the first element of the list, though this step is optional and can be skipped.
When you attempt to access items from a None value using indexing, the TypeError:’NoneType’ object is not subscriptable error is raised. A common mistake made by most developers when working with subscriptable objects such as lists, dictionaries, and tuples is to use the wrong syntax.
Every one of these built-in methods returns a null value, which cannot be assigned to a variable or indexed in any way. Let me know in the comment section if you are still facing this error I will be happy to help you.