The python error TypeError: ‘type’ object does not support item assignment occurs when an index value is inserted or changed in a variable assigned to a data type. The variable can only be accessed by using an index if it is a mutable collection of objects. If you try to change the index value for the variable assigned to the data type, the error TypeError: ‘type’ object does not support item assignment will be thrown in python.
The python variable can store a data type of an another variable. These variables can not be used to assign index value using assignment operator. The variable must be a mutable collection of objects to avoid the error TypeError: ‘type’ object does not support item assignment.
If the python variable contains a list of objects, the variable index may be used to access the object. The value in the index can be modified by the assignment operator. If the variable contains a data type of another variable, such as int, float, bool, list, set, etc., index access is not possible in these variables.
Exception
The error TypeError: ‘type’ 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 that contains value as a data type of another variable.
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
x[0] = 10
TypeError: 'type' object does not support item assignment
[Finished in 0.1s with exit code 1]
How to reproduce this issue
If you create a variable that is assigned to a data type of another variable and attempt to access an index value, this error can be reproduced. In the example below, an attempt is made to change the index value of index 0 in the variable using the assignment operator. That is why the error TypeError: ‘type’ object does not support item assignment will be thrown.
x = list
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: 'type' object does not support item assignment
[Finished in 0.1s with exit code 1]
Solution 1
If the variable that is assigned to a data type of another variable, there are chances of missing the code for the value assignment. The variable may be expected to store a collection of values, but the data type is assigned due to the coding problem. If the code is changed to assign the value, this error will be fixed.
a=(1,2,3)
x = list(a)
x[0] = 10
print x
Output
[10, 2, 3]
[Finished in 0.1s]
Solution 2
If the variable that is assigned to a data type of another variable, the index value assignment is not possible. Check the assignment of variables and assign them to a mutable collection such as list, set, array, etc. If the variable is assigned to an iterable object, the value can be changed using an index access. This is going to solve the error.
x = [1,2,3]
x[0] = 10
print x
Output
[10, 2, 3]
[Finished in 0.1s]
Solution 3
If you want to store the variable types in a list, create a list of the data types and store the value. Using the index to access the collection of data types.
x = [list] * 5
x[0] = int
print x
Output
[<type 'int'>, <type 'list'>, <type 'list'>, <type 'list'>, <type 'list'>]
[Finished in 0.0s]
Solution 4
If you want to create an empty list, store the value as received, then create an empty list. The value can be added using the command insert in the list.
x = []
x.insert(0,10)
print x
Output
[10]
[Finished in 0.0s]