If you subscript any object with None value, Python will raise TypeError: ‘NoneType’ object is not subscriptable exception. The term subscript means retrieving the values using indexing.
In this tutorial, we will learn what is NoneType object is not subscriptable error means and how to resolve this TypeError in your program with examples.
What is TypeError: ‘NoneType’ object is not subscriptable?
In Python, the objects that implement the
__getitem__ method are called subscriptable objects. For example, lists, dictionaries, tuples are all subscriptable objects. We can retrieve the items from these objects using Indexing.
The TypeError: ‘NoneType’ object is not subscriptable error is the most common exception in Python, and it will occur if you assign the result of built-in methods like
reverse() to a variable.
When you assign these methods to a variable, it returns a
None value. Let’s take an example and see if we can reproduce this issue.
numbers = [4, 5, 7, 1, 3, 6, 9, 8, 0] output = numbers.sort() print("The Value in the output variable is:", output) print(output)
The Value in the output variable is: None Traceback (most recent call last): File "c:PersonalIJSCodemain.py", line 9, in <module> print(output) TypeError: 'NoneType' object is not subscriptable
If you look at the above example, we have a list with some random numbers, and we tried sorting the list using a built-in
sort() method and assigned that to an output variable.
When we print the output variable, we get the value as None. In the next step, we are trying to access the element by indexing, thinking it is of type list, and we get TypeError: ‘NoneType’ object is not subscriptable.
You will get the same error if you perform other operations like
reverse(), etc., to the subscriptable objects like lists, dictionaries, and tuples. It is a design principle for all mutable data structures in Python.
TypeError: ‘NoneType’ object is not subscriptable Solution
Now that you have understood, we get the TypeError when we try to perform indexing on the
None Value. We will see different ways to resolve the issues.
Our above code was throwing TypeError because the
sort() method was returning None value, and we were assigning the None value to an output variable and indexing it.
The best way to resolve this issue is by not assigning the
sort() method to any variable and leaving the numbers.sort() as is.
Let’s fix the issue by removing the output variable in our above example and run the code.
numbers = [4, 5, 7, 1, 3, 6, 9, 8, 0] numbers.sort() output = numbers print("The Value in the output variable is:", output) print(output)
The Value in the output variable is: 3 3
If you look at the above code, we are sorting the list but not assigning it to any variable.
Also, If we need to get the element after sorting, then we should index the original list variable and store it into a variable as shown in the above code.
The TypeError: ‘int’ object is not subscriptable error is raised when you try to access items from a None value using indexing.
Most developers make this common mistake while manipulating subscriptable objects like lists, dictionaries, and tuples. All these built-in methods return a
None value, and this cannot be assigned to a variable and indexed.