Last updated: Apr 8, 2024
Reading time·4 min
The Python "TypeError: 'tuple' object does not support item assignment" occurs when we try to change the value of an item in a tuple.
To solve the error, convert the tuple to a list, change the item at the specific index and convert the list back to a tuple.
Here is an example of how the error occurs.
my_tuple = ('a', 'b', 'c') # ⛔️ TypeError: 'tuple' object does not support item assignment my_tuple[0] = 'z'
We tried to update an element in a tuple, but tuple objects are immutable which caused the error.
We cannot assign a value to an individual item of a tuple.
Instead, we have to convert the tuple to a list.
my_tuple = ('a', 'b', 'c') # ✅ Convert the tuple to a list my_list = list(my_tuple) print(my_list) # 👉️ ['a', 'b', 'c'] # ✅ Update the item my_list[0] = 'z' # ✅ Convert the list back to a tuple my_tuple = tuple(my_list) print(my_tuple) # 👉️ ('z', 'b', 'c')
This is a three-step process:
tuple()
class to convert the list back to a tuple.Once we have a list, we can update the item at the specified index and optionally convert the result back to a tuple.
Python indexes are zero-based, so the first item in a tuple has an index of 0
,
and the last item has an index of -1
or len(my_tuple) - 1
.
Alternatively, you can construct a new tuple that contains the updated element at the specified index.
def get_updated_tuple(tup, index, new_value): return tup[:index] + (new_value, ) + tup[index + 1:] my_tuple = ('a', 'b', 'c', 'd', 'e') index_to_update = 1 updated_value = 'Z' new_tuple = get_updated_tuple(my_tuple, index_to_update, updated_value) # 👇️ ('a', 'Z', 'c', 'd', 'e') print(new_tuple)
The get_updated_tuple
function takes a tuple, an index and a new value and
returns a new tuple with the updated value at the specified index.
The original tuple remains unchanged because tuples are immutable.
We updated the tuple element at index 1
, setting it to Z
.
If you only have to do this once, you don't have to define a function.
my_tuple = ('a', 'b', 'c', 'd', 'e') index_to_update = 1 updated_value = 'Z' # 👇️ notice that the updated_value is wrapped in a tuple new_tuple = my_tuple[:index_to_update] + \ (updated_value,) + my_tuple[index_to_update + 1:] # 👇️ ('a', 'Z', 'c', 'd', 'e') print(new_tuple)
The code sample achieves the same result without using a reusable function.
updated_value
in parentheses with a trailing comma.The values on the left and right-hand sides of the addition (+) operator have to all be tuples.
The syntax for
tuple slicing
is my_tuple[start:stop:step]
.
The start
index is inclusive and the stop
index is exclusive (up to, but not
including).
If the start
index is omitted, it is considered to be 0
, if the stop
index
is omitted, the slice goes to the end of the tuple.
Alternatively, you can declare a list from the beginning by wrapping the elements in square brackets (not parentheses).
my_list = ['a', 'b', 'c'] my_list[0] = 'z' print(my_list) # 👉️ ['z', 'b', 'c'] my_list.append('d') print(my_list) # 👉️ ['z', 'b', 'c', 'd'] my_list.insert(0, '.') print(my_list) # 👉️ ['.', 'z', 'b', 'c', 'd']
Declaring a list from the beginning is much more efficient if you have to change the values in the collection often.
Tuples are intended to store values that never change.
append()
method to add an item to the end of the list or the insert()
method to add an item at a specific index.In case you declared a tuple by mistake, tuples are constructed in multiple ways:
()
creates an empty tuplea,
or (a,)
a, b
or (a, b)
tuple()
constructorYou can also handle the error by checking if the value is a tuple before the assignment.
my_tuple = ('a', 'b', 'c') if isinstance(my_tuple, tuple): my_tuple = list(my_tuple) my_tuple[0] = 'Z' print(my_tuple) # 👉️ ['Z', 'b', 'c']
If the variable stores a tuple, we set it to a list to be able to update the value at the specified index.
The isinstance() function
returns True
if the passed-in object is an instance or a subclass of the
passed-in class.
If you aren't sure what type a variable stores, use the built-in type()
class.
my_tuple = ('a', 'b', 'c') print(type(my_tuple)) # 👉️ <class 'tuple'> print(isinstance(my_tuple, tuple)) # 👉️ True my_list = ['bobby', 'hadz', 'com'] print(type(my_list)) # 👉️ <class 'list'> print(isinstance(my_list, list)) # 👉️ True
The type class returns the type of an object.
You can learn more about the related topics by checking out the following tutorials: