TypeError: 'tuple' object does not support item assignment

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# TypeError: 'tuple' object does not support item assignment

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.

typeerror tuple object does not support item assignment

Here is an example of how the error occurs.

main.py
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.

# Convert the tuple to a list to solve the error

We cannot assign a value to an individual item of a tuple.

Instead, we have to convert the tuple to a list.

main.py
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')

convert tuple to list to solve the error

This is a three-step process:

  1. Use the list() class to convert the tuple to a list.
  2. Update the item at the specified index.
  3. Use the tuple() class to convert the list back to a tuple.
This is necessary because tuples are immutable, whereas lists are mutable.

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.

# Constructing a new tuple with the updated element

Alternatively, you can construct a new tuple that contains the updated element at the specified index.

main.py
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)

construct new tuple with updated element

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.

main.py
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.

Notice that we had to wrap 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.

# Using a list instead of a tuple

Alternatively, you can declare a list from the beginning by wrapping the elements in square brackets (not parentheses).

main.py
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']

using list instead of tuple

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.

You can use the append() method to add an item to the end of the list or the insert() method to add an item at a specific index.

# How tuples are constructed in Python

In case you declared a tuple by mistake, tuples are constructed in multiple ways:

  • Using a pair of parentheses () creates an empty tuple
  • Using a trailing comma - a, or (a,)
  • Separating items with commas - a, b or (a, b)
  • Using the tuple() constructor

# Checking if the value is a tuple

You can also handle the error by checking if the value is a tuple before the assignment.

main.py
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']

check if value is tuple

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.

main.py
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.