How to Solve Python TypeError: ‘tuple’ object does not support item assignment

by | Programming, Python, Tips

Tuples are immutable objects, which means you cannot change them once created. If you try to change a tuple in place using the indexing operator [], you will raise the TypeError: ‘tuple’ object does not support item assignment.

To solve this error, you can convert the tuple to a list, perform an index assignment then convert the list back to a tuple.

This tutorial will go through how to solve this error and solve it with the help of code examples.


TypeError: ‘tuple’ object does not support item assignment

Let’s break up the error message to understand what the error means. TypeError occurs whenever you attempt to use an illegal operation for a specific data type.

The part 'tuple' object tells us that the error concerns an illegal operation for tuples.

The part does not support item assignment tells us that item assignment is the illegal operation we are attempting.

Tuples are immutable objects, which means we cannot change them once created. We have to convert the tuple to a list, a mutable data type suitable for item assignment.

Example

Let’s look at an example of assigning items to a list. We will iterate over a list and check if each item is even. If the number is even, we will assign the square of that number in place at that index position.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for n in range(len(numbers)):
    if numbers[n] % 2 == 0:
        numbers[n] = numbers[n] ** 2
print(numbers)

Let’s run the code to see the result:

[1, 4, 3, 16, 5, 36, 7, 64, 9, 100]

We can successfully do item assignments on a list.

Let’s see what happens when we try to change a tuple using item assignment:

cities = ("Paris", "Montevideo", "New Delhi", "Miami", "Kaduna")
cities[2] = "Accra"

print(cities)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-c65ee33540a1> in <module>
      1 cities = ("Paris", "Montevideo", "New Delhi", "Miami", "Kaduna")
----> 2 cities[2] = "Accra"
      3 
      4 print(cities)

TypeError: 'tuple' object does not support item assignment

We throw the TypeError because the tuple object is immutable.

Solution

To solve this error, we need to convert the tuple to a list then perform the item assignment. We will then convert the list back to a tuple. However, you can leave the object as a list if you do not need a tuple.

cities = ("Paris", "Montevideo", "New Delhi", "Miami", "Kaduna")
cities_list = list(cities)
cities_list[2] = "Accra"
cities = tuple(cities_list)
print(cities)

Let’s run the code to see the updated tuple:

('Paris', 'Montevideo', 'Accra', 'Miami', 'Kaduna')

Solution

Congratulations on reading to the end of this tutorial. The TypeError: ‘tuple’ object does not support item assignment occurs when you try to change a tuple in-place using the indexing operator []. You cannot modify a tuple once you create it. To solve this error, you need to convert the tuple to a list, update it, then convert it back to a tuple.

For further reading on TypeErrors, go to the article:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee