11-12-2020, 09:04 PM
Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug)
Do you encounter this stupid error?
![[Image: image-20.png]](https://blog.finxter.com/wp-content/uploads/2020/11/image-20.png)
You’re not alone—thousands of coders like you generate this error in thousands of projects every single month. This short tutorial will show you exactly why this error occurs, how to fix it, and how to never make the same mistake again. So, let’s get started!
Python throws the if you use indexing with the square bracket notation on an object that is not indexable. This is the case if the object doesn’t define the method. You can fix it by removing the indexing call or defining the method.
The following code snippet shows the 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:
Note that a similar problem arises if you set the variable to the integer value instead of the value. The only difference is that the error message now is .
![[Image: image-19.png]](https://blog.finxter.com/wp-content/uploads/2020/11/image-19.png)
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 throw an error anymore.
An alternative is to define the method in your code:
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 . It doesn’t make a lot of sense here but is the minimal example that shows how it works.
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:
The post Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug) first appeared on Finxter.
Do you encounter this stupid error?
![[Image: image-20.png]](https://blog.finxter.com/wp-content/uploads/2020/11/image-20.png)
You’re not alone—thousands of coders like you generate this error in thousands of projects every single month. This short tutorial will show you exactly why this error occurs, how to fix it, and how to never make the same mistake again. So, let’s get started!
Python throws the
Code:
TypeError object is not subscriptableCode:
__getitem__()Code:
__getitem__The following code snippet shows the minimal example that leads to the error:
Code:
variable = None
print(variable[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:
- Indexing in Python
- Slicing in Python
- Highly Recommended: Accessing the Index of Iterables in Python
Note that a similar problem arises if you set the variable to the integer value
Code:
42Code:
NoneCode:
"TypeError: 'int' object is not subscriptable"![[Image: image-19.png]](https://blog.finxter.com/wp-content/uploads/2020/11/image-19.png)
You can fix the non-subscriptable TypeError by wrapping the non-indexable values into a container data type such as a listin Python:
Code:
variable = [None]
print(variable[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 0You overwrite the
Code:
__getitem__Code:
iCode:
selfCode:
iCode:
"Value 0"Code:
variable[0]Code:
"Value 10"Code:
variable[10]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:
The post Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug) first appeared on Finxter.

