The python error TypeError: ‘NoneType’ object does not support item assignment occurs when a value is inserted or changed using an index for a variable which is assigned with None. The value can be changed for a mutable list of objects. If you attempt to change the value of a variable that contains None, the error TypeError: ‘NoneType’ object does not support item assignment will be thrown in python.

The variable assigned to None is nothing but a variable not assigned to any object. These variables can not be accessed using an index. The index assignment can only be performed with a mutable collection of objects such as list, set, etc. If you attempt to assign a value to a variable assigned to None, the error TypeError: ‘NoneType’ object does not support item assignment will be thrown.

The variable must be assigned with a mutable collection such as list, set etc. These variables can be accessed using index of the elements. The error will not be thrown if you use indexes to change these variables.



Exception

The error TypeError: ‘NoneType’ object does not support item assignment will be shown as below the stack trace. The stack trace will display the line that the assignment operator is attempting to change a value in the variable.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    x[0] = 10
TypeError: 'NoneType' object does not support item assignment
[Finished in 0.1s with exit code 1]


How to reproduce this issue

If you attempt to change a value using the index of a variable assigned to None, this error can be reproduced. In the example below, an attempt is made to change the value in index 0 by using the assignment operator. The variable is actually assigned to None. That is why the error will be thrown.

x = None
x[0] = 10
print x

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    x[0] = 10
TypeError: 'NoneType' object does not support item assignment
[Finished in 0.1s with exit code 1]


Solution 1

The first step is to verify why the variable is assigned to the value None. The value might not be available. If a variable is assigned to a mutable collection of objects such as list, set, array, etc., the error TypeError: ‘NoneType’ object does not support item assignment will be fixed.

x = [1,2,3]
x[0] = 10
print x

Output

[10, 2, 3]
[Finished in 0.0s]


Solution 2

If the variable is assigned to None, skip assigning the value using the index. Accessing the variable with index is meaning less. I f the variable type is NoneType, skip the index value assignment.

x = None
if x is not None :
	x[0] = 10
print x

Output

None
[Finished in 0.0s]


Solution 3

If the variable is assigned to None and you intend to use it to store values in the variable, then create a list with a value assigned to None. The index would have been available in the list. The list is assigned to the value None that can be changed by adding the values to the index.

x = None
if x is None :
	x = [None] * 5
x[0] = 10
print x

Output

[10, None, None, None, None]
[Finished in 0.1s]



Leave a Reply