TypeError: 'NoneType' object is not subscriptable


How to fix TypeError: 'NoneType' object is not subscriptable
The error is self-explanatory. You are trying to subscript an object which you think is a list or dict, but actually is None. This means that you tried to do:
None[something]
NoneType is the type of the None object which represents a lack of value, for example, a function that does not explicitly return a value will return None . example
list1 = [1, 2] list1 = list.sort(list1) temp = list1[0]
you will get the error message :
Traceback (most recent call last): File "sample.py", line 3, in <module> temp = list1[0] TypeError: 'NoneType' object is not subscriptable

list1 = list.sort(list1) : - here, you are setting it to None. None always has no data and can not be subscriptable.

In order to correct this error this should be

list1 = [1, 2] list1.sort() temp = list1[0] print(temp)


python NoneType' object In general, the error means that you attempted to index an object that doesn't have that functionality. You might have noticed that the method sort() that only modify the list have no return value printed – they return the default None. This is a design principle for all mutable data structures in Python. This TypeError is the one thrown by python when you use the square bracket notation object[key] where an object doesn't define the __getitem__ method . You can reproduce TypeError that you get in your code if you try this at the Python command line:
>>> None[0] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'NoneType' object is not subscriptable >>>

Python typeerror

The Typeerror may be raised by user code to indicate that an attempted operation on an object is not supported, and is not meant to be. Passing arguments of the wrong type (e.g. passing a list when an int is expected) should result in a TypeError , but passing arguments with the wrong value (e.g. a number outside expected boundaries) should result in a ValueError . So you would get a TypeError if you tried list1 = list.sort(list1) because here you are setting it to None . None always has no data and can not be subscriptable.
how to fix python typeerror

Object is not subscriptable

A subscriptable object is any object that implements the __getitem__ special method (think lists, dictionaries). It is an object that records the operations done to it and it can store them as a "script" which can be replayed.