Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug)

#1
Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug)

Do you encounter this stupid error?

 TypeError: 'NoneType' object is not subscriptable

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 TypeError object is not subscriptable 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 __getitem__() method. You can fix it by removing the indexing call or defining the __getitem__ method.

The following code snippet shows the minimal example that leads to the error:

variable = None
print(variable[0])
# TypeError: 'NoneType' object is not subscriptable

You set the variable to the value None. The value None 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:

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 42 instead of the None value. The only difference is that the error message now is "TypeError: 'int' object is not subscriptable".

TypeError: 'int' object is not subscriptable

You can fix the non-subscriptable TypeError by wrapping the non-indexable values into a container data type such as a list in Python:

variable = [None]
print(variable[0])
# None

The output now is the value None and the script doesn’t throw an error anymore.

An alternative is to define the __getitem__ method in your code:

class X: def __getitem__(self, i): return f"Value {i}" variable = X()
print(variable[0])
# Value 0

You overwrite the __getitem__ method that takes one (index) argument i (in addition to the obligatory self argument) and returns the i-th value of the “container”. In our case, we just return a string "Value 0" for the element variable[0] and "Value 10" for the element variable[10]. 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.



https://www.sickgaming.net/blog/2020/11/...tupid-bug/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Pandas Series Object – A Helpful Guide with Examples xSicKxBot 0 1,311 05-01-2023, 01:30 AM
Last Post: xSicKxBot
  [Tut] Python TypeError: ‘dict_keys’ Not Subscriptable (Fix This Stupid Bug) xSicKxBot 0 1,215 10-17-2022, 03:24 AM
Last Post: xSicKxBot
  [Tut] (Fixed) Python TypeError ‘bool’ object is not subscriptable xSicKxBot 0 1,434 10-02-2022, 12:23 PM
Last Post: xSicKxBot
  [Tut] Python TypeError ‘set’ object is not subscriptable xSicKxBot 0 1,219 09-11-2022, 08:12 AM
Last Post: xSicKxBot
  [Tut] Python TypeError: NoneType is Not Subscriptable (Fix This Stupid Bug) xSicKxBot 0 1,125 09-06-2022, 02:00 AM
Last Post: xSicKxBot
  [Tut] [Fixed] Matplotlib: TypeError: ‘AxesSubplot’ object is not subscriptable xSicKxBot 0 1,305 09-05-2022, 09:33 AM
Last Post: xSicKxBot
  [Tut] How to Fix TypeError: unhashable type: ‘list’ xSicKxBot 0 1,349 07-14-2022, 03:51 AM
Last Post: xSicKxBot
  [Tut] How to Get The Current Reference Count of an Object in Python? xSicKxBot 0 1,408 12-21-2020, 05:13 AM
Last Post: xSicKxBot
  [Tut] Python Math Domain Error (How to Fix This Stupid Bug) xSicKxBot 0 1,256 11-21-2020, 05:01 AM
Last Post: xSicKxBot
  [Tut] Python’s NameError: name ‘xxx’ is not defined — How to Fix This Stupid Bug? xSicKxBot 0 1,218 10-16-2020, 07:30 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:

Forum software by © MyBB Theme © iAndrew 2016