TypeError: ‘float’ object does not support item assignment

The python error TypeError: ‘float’ object does not support item assignment occurs when a value is inserted or changed in a float variable using an index by the assignment operator. The float variable is an immutable value. The float value can not be accessed using the index. If you attempt to change the float value using index, the error TypeError: ‘float’ object does not support item assignment will be thrown in python.

The python variable will store a float value. If you want to store a lot of float values, you can use an iterable object such as list, set, array, etc. You can access this float value in iterable objects using the index value. If the float value is accessed by using the index in the float variable, the error TypeError: ‘float’ object does not support item assignment will be thrown.

If the python variable contains a list of the float value, the variable index may be used to access the float value. The float value in the index can be modified by the assignment operator. If the variable contains a single float value, it is not possible to access the index in this variable.



Exception

The error TypeError: ‘float’ 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 float value using index.

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


How to reproduce this error

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

x = 3.0
x[0] = 2.0
print x

Output

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


Solution 1

If you want to change a float value, you should assign a float value to the variable. The value of the index should not be used. In the example below, the float value is modified without the index being used.

x = 3.0
x = 2.0
print x

Output

2.0
[Finished in 0.1s]


Solution 2

If you want to modify the float value in a list of float values, you should have iterable objects, such as list, array, set, etc., create an iterable object and store all float values, then the float value can be accessed using the index in the collection.

x = [3.0]
x[0] = 2.0
print x

Output

[2.0]
[Finished in 0.1s]


Solution 3

If you want to create an empty list and start storing float values in a list, create iterable objects such as list, array, set, etc. You can add float values to iterable objects using the insert command.

x = []
x.insert(0,2.0)
print x

Output

[2.0]
[Finished in 0.0s]



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *