[Solved] TypeError: ‘NoneType’ Object is Not Subscriptable

You might have worked with list, tuple, and dictionary data structures, the list and dictionary being mutable while the tuple is immutable. They all can store values. And additionally, values are retrieved by indexing. However, there will be times when you might index a type that doesn’t support it. Moreover, it might face an error similar to the error TypeError: ‘NoneType’ object is not subscriptable.

list_example = [1, 2, 3, "random", "text", 5.64]
tuple_example = (1, 2, 3, "random", "text", 5.64)

print(list_example[4])
print(list_example[5])
print(tuple_example[0])
print(tuple_example[3])
List and tuple support indexing
List and tuple support indexing

What is a TypeError?

The TypeError occurs when you try to operate on a value that does not support that operation. The most common reason for an error in a Python program is when a certain statement is not in accordance with the prescribed usage. The Python interpreter immediately raises a type error when it encounters an error, usually along with an explanation.

Let’s reproduce the type error we are getting:

var = None
print(var[1])

On trying to index the var variable, which is of NoneType, we get an error. The ‘NoneType’ object is not subscriptable.

TypeError: 'NoneType' object is not subscriptable
TypeError: ‘NoneType’ object is not subscriptable

Why ‘NoneType’ object is not subscriptable?

Let’s break down the error we are getting. Subscript is another term for indexing. Likewise, subscriptable means an indexable item. For instance, a list, string, or tuple is subscriptable. None in python represents a lack of value for instance, when a function doesn’t explicitly return anything, it returns None. Since the NoneType object is not subscriptable or, in other words, indexable. Hence, the error ‘NoneType’ object is not subscriptable.

An object can only be subscriptable if its class has __getitem__ method implemented.

dir(list)

By using the dir function on the list, we can see its method and attributes. One of which is the __getitem__ method. Similarly, if you will check for tuple, strings, and dictionary, __getitem__ will be present.

__getitem__ method is required for an item to be subscriptable
__getitem__ method is required for an item to be subscriptable
dir(None)

However, if you try the same for None, there won’t be a __getitem__ method. Which is the reason for the type error.

__getitem__ method is not present in the case of NoneType
__getitem__ method is not present in the case of NoneType

Resolving the ‘NoneType’ object is not subscriptable

The ‘NoneType’ object is not subscriptable and generally occurs when we assign the return of built-in methods like sort(), append(), and reverse(). What is the common thing among them? They all don’t return anything. They perform in-place operations on a list. However, if we try to assign the result of these functions to a variable, then None will get stored in it. For instance, let’s look at their examples.

Example 1: sort()

list_example = [1, 11, 14, 10, 5, 3, 2, 15, 77]
list_example_sorted = list_example.sort()
print(list_example_sorted[0])

The sort() method sorts the list in ascending order. In the above code, list_example is sorted using the sort method and assigned to a new variable named list_example_sorted. On printing the 0th element, the ‘NoneType’ object is not subscriptable type error gets raised.

TypeError on using sort method
TypeError on using sort method

Recommended Reading | [Solved] TypeError: method Object is not Subscriptable

Example 2: append()

list_example = [1, 11, 14, 10, 5, 3, 2, 15, 77]
list_example_updated = list_example.append(88)
print(list_example_updated[5])

The append() method accepts a value. The value is appended to t. In the above code, the return value of the append is stored in the list_example_updated variable. On printing the 5th element, the ‘NoneType’ object is not subscriptable type error gets raised.

TypeError on using append method
TypeError on using the append method

Example 2: reverse()

Similar to the above examples, the reverse method doesn’t return anything. However, assigning the result to a variable will raise an error. Because the value stored is of NoneType.

list_example = [1, 11, 14, 10, 5, 3, 2, 15, 77]
list_example_reversed = list_example.reverse()
print(list_example_reversed[5])
TypeError on using reverse method
TypeError on using the reverse method

Recommended Reading | How to Solve TypeError: ‘int’ object is not Subscriptable

The solution to the ‘NoneType’ object is not subscriptable

It is important to realize that all three methods don’t return anything to resolve this error. This is why trying to store their result ends up being a NoneType. Therefore, avoid storing their result in a variable. Let’s see how we can do this, for instance:

Solution for sort() method

list_example = [1, 11, 14, 10, 5, 3, 2, 15, 77]
list_example.sort()
print(list_example[0])
TypeError for sort resolved
TypeError for sort resolved

Solution for append() method

list_example = [1, 11, 14, 10, 5, 3, 2, 15, 77]
list_example.append(88)
print(list_example[-1])
TypeError for append resolved
TypeError for append resolved

Solution for the reverse() method

list_example = [1, 11, 14, 10, 5, 3, 2, 15, 77]
list_example_reversed = list_example.reverse()
print(list_example_reversed[5])
TypeError for reverse resolved
TypeError for reverse resolved

TypeError: ‘NoneType’ object is not subscriptable, JSON/Django/Flask/Pandas/CV2

The error, NoneType object is not subscriptable, means that you were trying to subscript a NoneType object. This resulted in a type error. ‘NoneType’ object is not subscriptable is the one thrown by python when you use the square bracket notation object[key] where an object doesn’t define the __getitem__ method. Check your code for something of this sort.

None[something]

FAQs

How to catch TypeError: ‘NoneType’ object is not subscriptable?

This type of error can be caught using the try-except block. For instance:
try:
list_example = [1, 11, 14, 10, 5, 3, 2, 15, 77]
list_sorted = list_example.sort()
print(list_sorted[0])
except TypeError as e:
print(e)
print("handled successfully")

How can we avoid the ‘NoneType’ object is not subscriptable?

It is important to realize that Nonetype objects aren’t indexable or subscriptable. Therefore an error gets raised. Hence, in order to avoid this error, make sure that you aren’t indexing a NoneType.

Conclusion

This article covered TypeError: ‘NoneType’ object is not subscriptable. We talked about what is a type error, why the ‘NoneType’ object is not subscriptable, and how to resolve it.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments