Tuple object does not support item assignment как исправить

The following fragment of code from this tutorial: http://www.raywenderlich.com/24252/beginning-game-programming-for-teens-with-python

for badguy in badguys:
        if badguy[0]<-64:
            badguys.pop(index)
        badguy[0]-=7
        index+=1
    for badguy in badguys:
        screen.blit(badguyimg, badguy)

is giving me a :

TypeError: ‘tuple’ object does not support item assignment

I understand that this could be becuse badguy is a tuple. This means it is immutable(you can not change its values) Ive tried the following:

t= list(badguy)
        t[0]= t[0]-7
        i+=1

I converted the tuple to a list so we can minus 7. But in the game nothing happens.

Does any one know what I could do?

Thanks.

asked Oct 12, 2013 at 19:19

Pro-grammer's user avatar

0

Change this

badguy[0]-=7

into this

badguy = list(badguy)
badguy[0]-=7
badguy = tuple(badguy)

Alternatively, if you can leave badguy as a list, then don’t even use tuples and you’ll be fine with your current code (with the added change of using lists instead of tuples)

answered Oct 12, 2013 at 19:24

inspectorG4dget's user avatar

inspectorG4dgetinspectorG4dget

109k27 gold badges147 silver badges238 bronze badges

3

Another solution is instead of

badguy[0] -= 7

to do

badguy = (badguy[0] - 7,) + badguy[1:]

This creates a new tuple altogether with the updated value in the zeroth element.

answered Oct 12, 2013 at 21:06

SethMMorton's user avatar

SethMMortonSethMMorton

44.9k12 gold badges64 silver badges84 bronze badges

You can do a np.copy() and work with her.

badguy_copy = np.copy(badguy)

answered Feb 9, 2020 at 9:55

SSR's user avatar

Have you ever seen the error “tuple object does not support item assignment” when working with tuples in Python? In this article we will learn why this error occurs and how to solve it.

The error “tuple object does not support item assignment” is raised in Python when you try to modify an element of a tuple. This error occurs because tuples are immutable data types. It’s possible to avoid this error by converting tuples to lists or by using the tuple slicing operator.

Let’s go through few examples that will show you in which circumstances this error occurs and what to do about it.

Let’s get started!

Explanation of the Error “Tuple Object Does Not Support Item Assignment”

Define a tuple called cities as shown below:

cities = ('London', 'Paris', 'New York') 

If you had a list you would be able to update any elements in the list.

But, here is what happens if we try to update one element of a tuple:

>>> cities[1] = 'Rome'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment 

Tuples are immutable and that’s why we see this error.

But…

There is a workaround to this, we can:

  1. Convert the tuple into a list.
  2. Update any elements in the list.
  3. Convert the final list back to a tuple.

To convert the tuple into a list we will use the list() function:

>>> cities_list = list(cities)
>>> type(cities_list)
<class 'list'> 

Now, let’s update the element at index 1 in the same way we have tried to do before with the tuple:

>>> cities_list[1] = 'Rome'
>>> cities_list
['London', 'Rome', 'New York'] 

You can see that the second element of the list has been updated.

Finally, let’s convert the list back to a tuple using the tuple() function:

>>> tuple(cities_list)
('London', 'Rome', 'New York') 

Makes sense?

Avoid the “Tuple Object Does Not Support Item Assignment” Error with Slicing

The slicing operator also allows to avoid this error.

Let’s see how we can use slicing to create a tuple from our original tuple where only one element is updated.

We will use the following tuple and we will update the value of the element at index 2 to ‘Rome’.

cities = ('London', 'Paris', 'New York', 'Madrid', 'Lisbon') 

Here is the result we want:

('London', 'Paris', 'Rome', 'Madrid', 'Lisbon') 

We can use slicing and concatenate the first two elements of the original tuple, the new value and the last two elements of the original tuple.

Here is the generic syntax of the slicing operator (in this case applied to a tuple).

tuple_object[n:m]

This takes a slice of the tuple including the element at index n and excluding the element at index m.

Firstly, let’s see how to print the first two and last two elements of the tuple using slicing…

First two elements

>>> cities[0:2]
('London', 'Paris') 

We can also omit the first zero considering that the slice starts from the beginning of the tuple.

>>> cities[:2]
('London', 'Paris') 

Last two elements

>>> cities[3:]
('Madrid', 'Lisbon') 

Notice that we have omitted index m considering that the slice includes up to the last element of the tuple.

Now we can create the new tuple starting from the original one using the following code:

>>> cities[:2] + ('Rome',) + cities[3:]
('London', 'Paris', 'Rome', 'Madrid', 'Lisbon') 

(‘Rome’,) is a tuple with one element of type string.

Does “Tuple Object Does Not Support Item Assignment” Apply to a List inside a Tuple?

Let’s see what happens when one of the elements of a tuple is a list.

>>> values = (1, '2', [3]) 

If we try to update the second element of the tuple we get the expected error:

>>> values[1] = '3'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment 

If we try to assign a new list to the third element…

>>> values[2] = [3,4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment 

…once again we get back the error “‘tuple’ object does not support item assignment“.

But if we append another number to the list inside the tuple, here is what happens:

>>> values[2].append(4)
>>> values
(1, '2', [3, 4]) 

The Python interpreter doesn’t raise any exceptions because the list is a mutable data type.

This concept is important for you to know when you work with data types in Python:

In Python, lists are mutable and tuples are immutable.

How to Solve This Error with a List of Tuples

Do we see this error also with a list of tuples?

Let’s say we have a list of tuples that is used in a game to store name and score for each user:

users = [('John', 345), ('Mike', 23), ('Richard', 876)]

The user John has gained additional points and I want to update the points associated to his user:

>>> users[0][1] = 400
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment 

When I try to update his points we get back the same error we have seen before when updating a tuple.

How can we get around this error?

Tuples are immutable but lists are mutable and we could use this concept to assign the new score to a new tuple in the list, at the same position of the original tuple in the list.

So, instead of updating the tuple at index 0 we will assign a new tuple to it.

Let’s see if it works…

>>> users[0] = ('John', 400)
>>> users
[('John', 400), ('Mike', 23), ('Richard', 876)] 

It does work! Once again because a list is mutable.

And here is how we can make this code more generic?

>>> users[0] = (users[0][0], 400)
>>> users
[('John', 400), ('Mike', 23), ('Richard', 876)] 

Ok, this is a bit more generic because we didn’t have to provide the name of the user when updating his records.

This is just an example to show you how to address this TypeError, but in reality in this scenario I would prefer to use a dictionary instead.

It would allow us to access the details of each user from the name and to update the score without any issues.

Tuple Object Does Not Support Item Assignment Error With Values Returned by a Function

This error can also occur when a function returns multiple values and you try to directly modify the values returned by the function.

I create a function that returns two values: the number of users registered in our application and the number of users who have accessed our application in the last 30 days.

>>> def get_app_stats():
...     users_registered = 340
...     last_30_days_logins = 2003
...     return users_registered, last_30_days_logins
... 
>>> stats = get_app_stats()
>>> stats
(340, 2003) 

As you can see the two values are returned by the function as a tuple.

So, let’s assume there is a new registered user and because of that I try to update the value returned by the function directly.

I get the following error…

>>> stats[0] = stats[0] + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment 

This can happen especially if I know that two values are returned by the function but I’m not aware that they are returned in a tuple.

Why Using Tuples If We Get This Error?

You might be thinking…

What is the point of using tuples if we get this error every time we try to update them?

Wouldn’t be a lot easier to always use lists instead?

We can see the fact that tuples are immutable as an added value for tuples when we have some data in our application that should never be modified.

Let’s say, for example, that our application integrates with an external system and it needs some configuration properties to connect to that system.

ext_system_config = ('api.ext.system.com', '443')

The tuple above contains two values: the API endpoint of the system we connect to and the port for their API.

We want to make sure this configuration is not modified by mistake in our application because it would break the integration with the external system.

So, if our code inadvertently updates one of the values, the following happens:

>>> ext_system_config[0] = 'incorrect_value'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment 

Remember, it’s not always good to have data structures you can update in your code whenever you want.

Conclusion

In this article we have seen when the error “tuple object does not support item assignment” occurs and how to avoid it.

You have learned how differently the tuple and list data types behave in Python and how you can use that in your programs.

If you have any questions feel free to post them in the comment below 🙂

I’m a Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!

Here’s everything about TypeError: ‘Tuple’ Object Does Not Support Item Assignment in Python.

You’ll learn:

  • The specifics of the tuple data type
  • The difference between immutable and mutable data types
  • How to change immutable data types
  • Lots more

So if you want to understand this error in Python and how to solve it, then you’re in the right place.

Let’s jump right in!

Polygon art logo of the programming language Python.

Mutable, or Immutable? That Is the Question

Data types in Python are mutable or immutable.

All data types that are numeric, for example, are immutable

You can write something like this:

a = 1
a
1

And:

a = a + 1
a
2

Have you changed the variable a

Not really: When you write a = 1, you put the object 1 in memory and told the name a to refer to this literal. 

Next, when you write a = a + 1, Python evaluates the expression on the right:

Python takes the object referred by a (the 1) and then adds 1 to it. 

You get a new object, a 2. This object goes right into the memory and a references instead of object 1

The value of object 1 has not changed—it would be weird if 1 would out of a sudden a 2, for example, wouldn’t it? So instead of overwriting an object (1), a new object (2) is created and assigned to the variable (a).

Mutable Data Types

More complex data types in Python are sequences such as: 

  • Strings
  • Tuples
  • Bytes
  • Lists
  • Byte Arrays

Sequences contain several values, which can be accessed by index.

Software developer standing near his desk while working in a hurry.

However, some sequences are mutable (byte arrays, lists), while others are immutable (tuples)

You can create a tuple and access its elements like this:

tup1 = (1, "two", [3])
tup1[1]
two

Yet if you try to change one of the elements, you get an error:

tup1[1] = '2'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-903ae2cb95b0> in <module>()
----> 1 tup1[1] = '2'

TypeError: 'tuple' object does not support item assignment

Notice that the item in the tuple at index 2 is a list. You can change the list without changing the tuple:

tup1[2].append(4)
tup1
(1, 'two', [3, 4])

The object stored in the tuple remains the same, but its contents have changed. But what if you still need to change the element in the tuple?

You can do this by converting the tuple to a list. Then you change the element, and then convert the list to a tuple again:

tup1 = list(tup1)
tup1[0] = 'uno'
tup1 = tuple(tup1)
tup1
('uno', 'two', [3, 4])

For large amounts of data, conversion operations can take quite a long time:

import random
import time

tup2 = tuple(random.random() for _ in range(100_000_000))

t = time.process_time()
tup2 = list(tup2)
elapsed_time = time.process_time() - t
print('tuple->list: ', elapsed_time)

tup2[0] = random.random()

t = time.process_time()
tup2 = tuple(tup2)
elapsed_time = time.process_time() - t
print('list->tuple: ', elapsed_time)
tuple->list:  0.8301777420000036
list->tuple:  0.9393838999999957

As you can see, for a list of 100 million float numbers, this operation takes about a second. This is not a long time for most tasks, but it is still worth considering if you are dealing with large amounts of data.

However, there is another way to “change” a tuple element—you can rebuild a tuple using slicing and concatenation:

tup1 = (1, "two", [3])
tup1 = ('uno',) + tup1[1:]
tup1

Note that it is necessary to put a comma in parentheses to create a tuple of one element. If you use just parentheses, then (‘uno’) is not a tuple, but a string in parentheses

Concatenating a string with a tuple is not possible:

tup1 = ('uno') + tup1[1:]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-c51d4ed19b5a> in <module>()
----> 1 tup1 = ('uno') + tup1[1:]

TypeError: must be str, not tuple

Interestingly, you can use shorthand operators on a tuple, like this:

tup = (1, 2)
tup += (3, 4, 5)
tup
(1, 2, 3, 4, 5)

Or even like this:

tup = (1, 2)
tup *= 3
tup
(1, 2, 1, 2, 1, 2)

3 Examples of TypeError: ‘Tuple’ Object Does Not Support Item Assignment in Python

Let’s look at some practical examples of when this error can occur. The simplest is when you initially enter the sequence incorrectly:

list1 = (1, 2, 3)
list1[0] = 'one'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-da9ebc4ef661> in <module>()
      1 list1 = (1, 2, 3)
----> 2 list1[0] = 'one'

TypeError: 'tuple' object does not support item assignment

In this example, the name list1 refers to a tuple despite the list in the name. The name does not affect the type of variable. To fix this error, simply change the parentheses to square brackets in the constructor:

list1 = [1, 2, 3]
list1[0] = 'one'
list1
['one', 2, 3]

Perhaps you have a list with some values, such as the student’s name and grade point average:

grades = [('Alice', 98), ('Bob', 65), ('Carol', 87)]

Alice did a poor job this semester, and her GPA dropped to 90:

grades[0][1] = 90
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-dc371b5fb12e> in <module>()
----> 1 grades[0][1] = 90

TypeError: 'tuple' object does not support item assignment

Unfortunately, you cannot just change the average score in such a list. You already know that you can convert a tuple to a list, or form a new tuple. For example, like this:

grades[0] = (grades[0][0], 90)
grades
[('Alice', 90), ('Bob', 65), ('Carol', 87)]

However, if you need to change values regularly, it makes sense to switch from a list of tuples to a dictionary. Dictionaries are a perfect fit for such tasks. You can do this easily with the dict() constructor:

grades = [('Alice', 98), ('Bob', 65), ('Carol', 87)]
grades = dict(grades)
grades
{'Alice': 98, 'Bob': 65, 'Carol': 87}

Now you can change the average by student name:

grades['Alice'] = 90
grades
{'Alice': 90, 'Bob': 65, 'Carol': 87}

#1 Real World Example of TypeError: ‘Tuple’ Object Does Not Support Item Assignment in Python

An interesting example of a novice programmer trying to enter values in a list from the keyboard using the eval() function:

def my_sort(list):
  for index in range(1, len(list)):
    value = list[index]
    i = index-1
    while i>=0:
      if value <list[i]:
        list[i +1] = list[i]
        list[i] = value
        i = i-1
      else:
        break
  return
 
input_list = eval(input("Enter list items:"))
my_sort (input_list)
print(input_list)
Enter list items:3, 2, 4, 1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-a1ba042b65c4> in <module>()
     13 
     14 input_list = eval(input("Enter list items:"))
---> 15 my_sort (input_list)
     16 print(input_list)

<ipython-input-16-a1ba042b65c4> in my_sort(list)
      5     while i>=0:
      6       if value <list[i]:
----> 7         list[i +1] = list[i]
      8         list[i] = value
      9         i = i-1

TypeError: 'tuple' object does not support item assignment

This method is not very reliable by itself.

Even if the user enters the correct sequence separated by commas—for example, 3, 2, 4, 1—it will be evaluated in a tuple. 

Naturally, an attempt to assign a new value to a tuple element in the line list[i +1] = list[i] raises a TypeError: ‘tuple’ object does not support item assignment

Here, you see another mistake—which, by the way, may even be invisible during program execution. 

The my_sort function uses the list data type name as the argument name. This is not only the name of the data type, but also the list constructor. 

Python will not throw an error while executing this code, but if you try to create a list using the constructor inside the my_sort function, you will have big problems.

Programmer trying to solve problems with the code he's working on.

In this case, to enter elements into the list, it would be more correct to read the entire string and then split it using the split() method. If you need integer values, you can also apply the map() function, then convert the resulting map object into a list:

input_list = list (map(int, input("Enter the list items: ") .split ()))
input_list
Enter the list items: 4 2 3 1
[4, 2, 3, 1]

The construction looks a little cumbersome, but it does its job. You can also enter list items through a list comprehension:

input_list = [int (x) for x in input("Enter the list items: ") .split ()]
input_list
Enter the list items: 4 2 3 1
[4, 2, 3, 1]

You can choose the design that you like best.

#2 Real World Example of TypeError: ‘Tuple’ Object Does Not Support Item Assignment in Python

Another example of when a TypeError: ‘tuple’ object does not support item assignment may occur is the use of various libraries. 

If you have not studied the documentation well enough, you may not always clearly understand which data type will be returned in a given situation. In this example, the author tries to make the picture redder by adding 20 to the red color component:

from PIL import Image
image = Image.open('balloon.jpg')
pixels = list (image.getdata ())
for pixel in pixels: 
    pixel[0] = pixel[0] + 20    
image.putdata(pixels)
image.save('new.bmp')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-2e305d7cf9e6> in <module>()
      3 pixels = list (image.getdata ())
      4 for pixel in pixels:
----> 5     pixel[0] = pixel[0] + 20
      6 image.putdata(pixels)
      7 image.save('new.bmp')

TypeError: 'tuple' object does not support item assignment

This produces an error on the line pixel[0] = pixel[0] + 20. How?

You are converting pixels to a list in line of code 3. Indeed, if you check the type of the pixels variable, you get a list:

type(pixels)
list

However, in the loop, you iterate over the pixels list elements, and they already have a different type. Check the type of the pixels list element with index 0:

type(pixels[0])
tuple

And this is a tuple!

So, you can solve this problem by converting lists to tuples inside a loop, for example.

However, in this case, you will need to slightly adjust the iterable value. This is because you will need the pixel color values and the index to write the new values into the original array. 

For this, use the enumerate() function:

for i, pixel in enumerate(pixels): 
    pixel = list (pixel)
    pixel [0] = pixel [0] + 20
    pixels [i] = tuple (pixel)

The program will work successfully with that version of a loop, and you will get a redder image at the output. It would be more correct to trim values above 255, for example:

pixel[0] = min(pixel [0] + 20, 255)

But if the program consists only of this transformation, then Python will already truncate the values when saving the image.

Here’s more Python support:

  • 9 Examples of Unexpected Character After Line Continuation Character
  • 3 Ways to Solve Series Objects Are Mutable and Cannot be Hashed
  • How to Solve SyntaxError: Invalid Character in Identifier
  • ImportError: Attempted Relative Import With No Known Parent Package
  • IndentationError: Unexpected Unindent in Python (and 3 More)

The TypeError: ‘tuple’ object does not support item assignment error occurs when you try to change the value in the tuple by using the item assignment operator that does not support. The python tuple is an immutable object. If a tuple has been created, you can’t change the tuple. If you attempt to change the value of the tuple, the error TypeError: ‘tuple’ object does not support item assignment will be thrown in python.

A tuple is a collection of python objects. If a tuple has been created, the elements in the tuple can not be modified. If you want to alter the tuple, a new tuple will be created with the changes. After creating a new tuple, the original tuple would be removed. The Error TypeError: ‘tuple’ object does not support item assignment will be thrown if the current tuple is changed by the assignment operator

You can read the elements in the tuple using the tuple index. If the tuple is changed using an index value, the error TypeError: ‘tuple’ object does not support item assignment will be thrown. Tuple will not allow any element to be added, removed or changed.

Exception

The error TypeError: ‘tuple’ object does not support item assignment will be shown as below the stack trace. The stack trace will display the line that the assignment operator is attempting to change a value in the tuple.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    x[0] = 10
TypeError: 'tuple' object does not support item assignment
[Finished in 0.1s with exit code 1]

How to reproduce this issue

If you try to change an element in a tuple using the assignment operator, this error can be repeated. In the example below, an attempt is made to change the value in index 0 in the tuple using the assignment operator. That is why the error will be thrown.

x = (1,2,3)
x[0] = 10
print x

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    x[0] = 10
TypeError: 'tuple' object does not support item assignment
[Finished in 0.1s with exit code 1]

Solution 1

If you need to change values in a tuple, the tuple will be converted to a list of values, and then the value will be changed and converted back to a tuple. The list is a mutable object, which can be modified at any time. The tuple is an immutable object that can not be modified once it has been created. Therefore, before changing the values, the tuple is converted to a list.

x = (1,2,3)
y = list(x)
y[0] = 10
x = tuple(y)
print x

Output

(10, 2, 3)
[Finished in 0.1s]

Solution 2

If you need to change a value in a tuple, converting it to a list and converting it back to a tuple is not a good choice. The tuple slice is used to split and incorporate changes to the tuple. The tuple is an immutable object that can not be modified once it has been created. The tuple slice is therefore a choice to change a value.

x = (1,2,3)
index = 1
x = x[:index] + (10,) + x[index + 1:]
print x

Output

(1, 10, 3)
[Finished in 0.1s]

Solution 3

The tuple is an immutable object that can not be modified once it has been created. If you need to change the values later in time, create a list instead of a tuple. The list is a mutable object, which can be modified at any time. At any time, you can convert to a tuple.

x = [1,2,3]
x[0] = 10
print x
x = tuple(x)
print x

Output

[10, 2, 3]
(10, 2, 3)
[Finished in 0.1s]

Solution 4

If you are not sure about the object, check the object first. If the object is a mutable object, change the value of the object. Otherwise, take an alternate flow of the code.

x = (1,2,3)
if type(x) is tuple : 
	x = list(x)
x[0] = 10
print x

Output

(1, 2, 3)
[10, 2, 3]
[Finished in 0.1s]

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.

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

Select your interest

First name

Last name

Email

Phone number

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

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[0] = "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[3] = "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[3] = "Philosophy"
TypeError: 'tuple' object does not support item assignment

Our code returns an error.

The Solution

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 list() method:

classes = ("Chemistry", "Politics", "Biology", "Psychology")
as_list = list(classes)

as_list[3] = "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 tuple() method:

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.

Conclusion

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!

Понравилась статья? Поделить с друзьями:
  • Как найти массу здания
  • Как найти потенциал внутри сферы
  • Как составить вопросительное предложение в английском языке в будущем времени
  • Гадалка как найти где живет
  • Как исправить осанку у взрослого человека сутулость отзывы