Tuples are immutable objects. “Immutable” means you cannot change the values inside a tuple. You can only remove them. If you try to assign a new value to an item in a variable, you’ll encounter the “typeerror: ‘tuple’ object does not support item assignment” error.
In this guide, we discuss what this error means and why you may experience it. We’ll walk through an example of this error so you can learn how to solve it in your code.
typeerror: ‘tuple’ object does not support item assignment
While tuples and lists both store sequences of data, they have a few distinctions. Whereas you can change the values in a list, the values inside a tuple cannot be changed. Also, tuples are stored within parenthesis whereas lists are declared between square brackets.
Because you cannot change values in a tuple, item assignment does not work.
Consider the following code snippet:
honor_roll = ["Bill", "Jeff", "Lucy", "Lindsay"] honor_roll = "Holly"
This code snippet lets us change the first value in the “honor_roll” list to Holly. This works because lists are mutable. You can change their values. The same code does not work with data that is stored in a tuple.
An Example Scenario
Let’s build a program that tracks the courses offered by a high school. Students in their senior year are allowed to choose from a class but a few classes are being replaced.
Start by creating a collection of class names:
classes = ("Chemistry", "Politics", "Biology", "Psychology")
We’ve created a tuple that stores the names of each class being offered.
The science department has notified the school that psychology is no longer being offered due to a lack of numbers in the class. We’re going to replace psychology with philosophy as the philosophy class has just opened up a few spaces.
To do this, we use the assignment operator:
classes = "Philosophy"
This code will replace the value at the index position 3 in our list of classes with “Philosophy”. Next, we print our list of classes to the console so that the user can see what classes are being actively offered:
print("The classes being offered are: ") for c in classes: print(c)
Use a for loop to print out each class in our tuple to the console. Let’s run our code and see what happens:
Traceback (most recent call last): File "main.py", line 3, in <module> classes = "Philosophy" TypeError: 'tuple' object does not support item assignment
Our code returns an error.
We’ve tried to use the assignment operator to change a subject in our list. Tuples are immutable so we cannot change their values. This is why our code returns an error.
To solve this problem, we convert our “classes” tuple into a list. This will let us change the values in our sequence of class names.
Do this using the
classes = ("Chemistry", "Politics", "Biology", "Psychology") as_list = list(classes) as_list = "Philosophy" print("The classes being offered are: ") for c in as_list: print(c)
We use the
list() method to convert the value of “classes” to a list. We assign this new list to the variable “as_list”. Now that we have our list of classes stored as a list, we can change existing classes in the list.
Let’s run our code:
The classes being offered are: Chemistry Politics Biology Philosophy
Our code successfully changes the “Psychology” class to “Philosophy”. Our code then prints out the list of classes to the console.
If we need to store our data as a tuple, we can always convert our list back to a tuple once we have changed the values we want to change. We can do this using the
as_tuple = tuple(as_list) print(as_tuple)
This code converts “as_list” to a tuple and prints the value of our tuple to the console:
('Chemistry', 'Politics', 'Biology', 'Philosophy')
We could use this tuple later in our code if we needed our class names stored as a tuple.
The “typeerror: ‘tuple’ object does not support item assignment” error is raised when you try to change a value in a tuple using item assignment.
To solve this error, convert a tuple to a list before you change the values in a sequence. Optionally, you can then convert the list back to a tuple.
Now you’re ready to fix this error in your code like a pro!