PythonTypeError: Tuple object does not support item assignment.














































PythonTypeError: Tuple object does not support item assignment.



Python TypeError: 'Tuple' object does not support Item Assignment


TUPLE:

In Python, tuple is sequence of objects which are separated by commas. It can be used to 

group related data together.


IMMUTABLE:

Tuples are immutable which means, once tuple is created it's data value cannot be modified.

They are defined using parenthesis.

Tuple=('a','b','c')

To create a tuple with single value we must include 'comma' or otherwise python would not 

treat the type as tuple.

Tup=(1,) #type- 'tuple'
Tup=(1)  #type- 'Int'


Changing tuple values:

Once Python has created a tuple in memory, it cannot be changed. If we try to use 

assignment to modify the value of the tuple, we get an error as:

TypeError: 'tuple' object does not support item assignment

Consider the example which shows the few operations that can be performed on tuple.


Initially, we created a tuple with different data elements and printed values by iterating through the items using 'for' loop.

When we try to assign the value of tuple with new value using item assignment, it shows 'TypeError'.


Note:You can convert the tuple into list and then change the values in the list, convert back

list to tuple.


For example:


The output will be:



Read more about "List' in following link:

https://docs.python.org/3/tutorial/datastructures.html


Comments