09-06-2022, 02:00 AM
Python TypeError: NoneType is Not Subscriptable (Fix This Stupid Bug)
5/5 – (1 vote)
Do you encounter the following error message?
You’re not alone! This short tutorial will show you why this error occurs, how to fix it, and how to never make the same mistake again.
So, let’s get started!
Summary
Python raises the if you try to index or slice a value. The type is not indexable, i.e., it doesn’t define the method. You can fix it by removing the indexing or slicing call, or defining the method.
Example
![[Image: image-20.png]](https://blog.finxter.com/wp-content/uploads/2020/11/image-20.png)
The following minimal example that leads to the error:
You set the to the value . The value is not a container object, it doesn’t contain other objects. So, the code really doesn’t make any sense—which result do you expect from the indexing operation?
Exercise: Before I show you how to fix it, try to resolve the error yourself in the following interactive shell:
https://trinket.io/embed/python/e55b915062
If you struggle with indexing in Python, have a look at the following articles on the Finxter blog—especially the third!
Related Articles:
Fixes
You can fix the non-subscriptable TypeError by wrapping the non-indexable values into a container data type such as a listin Python:
The output now is the value and the script doesn’t yield an error message anymore.
An alternative is to define the method in your code:
Related Tutorial: Python magic method
You overwrite the method that takes one (index) argument (in addition to the obligatory argument) and returns the -th value of the “container”.
In our case, we just return a string for the element and for the element .
Full Guide: Python Fixing This Subsctiptable Error (General)
What’s Next?
I hope you’d be able to fix the bug in your code! Before you go, check out our free Python cheat sheets that’ll teach you the basics in Python in minimal time:
5/5 – (1 vote)
Do you encounter the following error message?
Code:
TypeError: NoneType is not subscriptableYou’re not alone! This short tutorial will show you why this error occurs, how to fix it, and how to never make the same mistake again.
So, let’s get started!
Summary
Python raises the
Code:
TypeError: NoneType is not subscriptableCode:
x[i]Code:
x[i:j]Code:
NoneCode:
NoneCode:
__getitem__()Code:
__getitem__Example
![[Image: image-20.png]](https://blog.finxter.com/wp-content/uploads/2020/11/image-20.png)
The following minimal example that leads to the error:
Code:
x = None
print(x[0])
# TypeError: 'NoneType' object is not subscriptableYou set the
Code:
variableCode:
NoneCode:
NoneExercise: Before I show you how to fix it, try to resolve the error yourself in the following interactive shell:
https://trinket.io/embed/python/e55b915062
If you struggle with indexing in Python, have a look at the following articles on the Finxter blog—especially the third!
Related Articles:Fixes
You can fix the non-subscriptable TypeError by wrapping the non-indexable values into a container data type such as a listin Python:
Code:
x = [None]
print(x[0])
# NoneThe output now is the value
Code:
NoneAn alternative is to define the
Code:
__getitem__()Code:
class X: def __getitem__(self, i): return f"Value {i}" variable = X()
print(variable[0])
# Value 0
Related Tutorial: Python Code:
__getitem__()You overwrite the
Code:
__getitem__Code:
iCode:
selfCode:
iIn our case, we just return a string
Code:
"Value 0"Code:
variable[0]Code:
"Value 10"Code:
variable[10]
Full Guide: Python Fixing This Subsctiptable Error (General)What’s Next?
I hope you’d be able to fix the bug in your code! Before you go, check out our free Python cheat sheets that’ll teach you the basics in Python in minimal time:


![[Image: 2b50.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/2b50.png)