Python TypeError: 'tuple' object does not support item assignment Solution

Posted in /  

Python TypeError: 'tuple' object does not support item assignment Solution
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    In Python, we have a built-in data structure " tuple " which is similar to a Python list and stores elements in sequential order. The only difference between a Python list and a tuple is that the tuple is an immutable data structure, which means once a tuple object is defined, we can not change its elements. If we try to change the tuple elements using indexing and assignment operator, we will receive the TypeError: 'tuple' object does not support item assignment Error.

    In this Python guide, we will discuss this error in detail and learn how to debug it. We will also walk through a common example to demonstrate this error. So without further ado, let's get started with the Error statement.

    Python Error: TypeError: 'tuple' object does not support item assignment

    The Error Statement TypeError: 'tuple' object does not support item assignment is divided into two parts Exception Type and Error Message.

    1. TypeError (Exception Type)
    2. 'tuple' object does not support item assignment (Error Message)

    1. TypeError

    TypeError is a standard Python exception. It is raised in a Python program when we try to perform an invalid or unsupported operation on a Python object.

    2. 'tuple' object does not support item assignment

    This error message tells us that the tuple object does not support new value assignment to its elements. You will only encounter this error message when you try to change the values of a tuple element using indexing.

    Example

    Although we can use indexing to access the individual tuple elements, we can not use indexing to change tuple element values.

    # initialize a tuple object
    letters = ('a', 'b', 'c', 'e')
    
    # try to change the tuple elements (error)
    letters[3] = 'd'
    
    print(letters)

    Output

    Traceback (most recent call last):
      File "main.py", line 5, in 
        letters[3] = 'd'
    TypeError: 'tuple' object does not support item assignment

    Here we are getting this error because in line 5, we are trying to assign a new value to the tuple " letters ". As tuple does not support element change operation, it throws the error when we try to assign a new value to the tuple element. Now let's discuss a common scenario example, and in the solution, we will see an alternative way to debug this problem and add and change the value of a tuple element using some logic.

    Common Example Scenario

    Tuples are faster as compared to the Python list. That's why many Python developers use tuples to store items or element values. Although tuple supports element access using indexing, it throws an error when changing its elements. This is also one of the main reasons why many pythoneer use tuples instead of lists when they do not want to change the elements of the container throughout the program.

    But let's say you come across a situation when you want to change the element value of a tuple, then what would you do?

    Example

    Here we will discuss an example where we first try to change one of the values of a tuple using indexing. Then in the solution, we will discuss how we can achieve it.

    # initialize tuple
    sem_1_subjects = ("Java", "Operating System", "Data Structure & Algo", "Data Base Management System", "Networking System")
    
    # change first value of the tuple
    sem_1_subjects[0] = "Python"
    
    print(sem_1_subjects)

    Output

    Traceback (most recent call last):
        File "main.py", line 5, in <module>
          sem_1_subjects[0] = "Python"
    TypeError: 'tuple' object does not support item assignment

    Break the code

    The error statement is quite expected. We know that when we try to change the tuple element value using indexing, we get the error. In line 5, we tried to change the first element of the tuple " sem_1_subjects " from "Java" to "Python" , which is the cause of this error statement.

    Solution

    When we use the tuple element as a container to store elements, we think of that container as intact throughout the program. But in the case when we come across a situation where we need to change the value of the tuple elements, there we first need to convert that tuple object to a list using list() function. Then only we can change its values. After changing the value, we can convert back the list object to the tuple using tuple() function.

    Example solution

    # initialize tuple
    sem_1_subjects = ("Java", "Operating System", "Data Structure & Algo", "DataBase Management System", "Networking System")
    
    # change the tuple to list
    sem_1_subjects = list(sem_1_subjects)
    
    # change first value of list
    sem_1_subjects[0] = "Python"
    
    # convert the list back to tuple
    sem_1_subjects = tuple(sem_1_subjects)
    
    print(sem_1_subjects)

    Output

    ('Python', 'Operating System', 'Data Structure & Algo', 'DataBase Management System', 'Networking System')

    Conclusion

    In this Python tutorial, we discussed the "TypeError: 'tuple' object does not support item assignment" Error in detail. This error raises in a Python program when we try to change the value of a tuple element using the assignment operator. A tuple is an immutable data structure, and once we define all its elements, we can not change them. To change its elements, first, need to convert the tuple object to a list, and then only we can change its values.

    If you are still getting this error in your Python program, you can share your code in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments