TypeError: ‘tuple’ object does not support item assignment

The python error TypeError: ‘tuple’ object does not support item assignment occurs when a value is inserted or changed in a tuple by the assignment operator. The python tuple is an immutable object. If a tuple has been created, you can’t change the tuple. If you attempt to change the value of the tuple, the error TypeError: ‘tuple’ object does not support item assignment will be thrown in python.

A tuple is a collection of python objects. If a tuple has been created, the elements in the tuple can not be modified. If you want to alter the tuple, a new tuple will be created with the changes. After creating a new tuple, the original tuple would be removed. The Error TypeError: ‘tuple’ object does not support item assignment will be thrown if the current tuple is changed by the assignment operator

You can read the elements in the tuple using the tuple index. If the tuple is changed using an index value, the error TypeError: ‘tuple’ object does not support item assignment will be thrown. Tuple will not allow any element to be added, removed or changed.



Exception

The error TypeError: ‘tuple’ 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 tuple.

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


How to reproduce this issue

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

x = (1,2,3)
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: 'tuple' object does not support item assignment
[Finished in 0.1s with exit code 1]


Solution 1

If you need to change values in a tuple, the tuple will be converted to a list of values, and then the value will be changed and converted back to a tuple. The list is a mutable object, which can be modified at any time. The tuple is an immutable object that can not be modified once it has been created. Therefore, before changing the values, the tuple is converted to a list.

x = (1,2,3)
y = list(x)
y[0] = 10
x = tuple(y)
print x

Output

(10, 2, 3)
[Finished in 0.1s]


Solution 2

If you need to change a value in a tuple, converting it to a list and converting it back to a tuple is not a good choice. The tuple slice is used to split and incorporate changes to the tuple. The tuple is an immutable object that can not be modified once it has been created. The tuple slice is therefore a choice to change a value.

x = (1,2,3)
index = 1
x = x[:index] + (10,) + x[index + 1:]
print x

Output

(1, 10, 3)
[Finished in 0.1s]


Solution 3

The tuple is an immutable object that can not be modified once it has been created. If you need to change the values later in time, create a list instead of a tuple. The list is a mutable object, which can be modified at any time. At any time, you can convert to a tuple.

x = [1,2,3]
x[0] = 10
print x
x = tuple(x)
print x

Output

[10, 2, 3]
(10, 2, 3)
[Finished in 0.1s]


Solution 4

If you are not sure about the object, check the object first. If the object is a mutable object, change the value of the object. Otherwise, take an alternate flow of the code.

x = (1,2,3)
if type(x) is tuple : 
	x = list(x)
x[0] = 10
print x

Output

(1, 2, 3)
[10, 2, 3]
[Finished in 0.1s]



Related Articles

Leave a Reply

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