How to Resolve Python "TypeError: '...' object does not support item assignment"
The TypeError: '...' object does not support item assignment
(where '...' could be str
, int
, float
, numpy.float64
, etc.) is a common error in Python. It occurs when you attempt to modify an element of an immutable object using index assignment ([] = ...
).
This guide explains why this error happens with strings, integers, and floats, and provides correct ways to achieve your intended outcome.
Understanding Immutability and Item Assignment
In Python, some data types are immutable, meaning their internal state can not be changed after they are created. Strings (str
), integers (int
), and floating-point numbers (float
), including NumPy's numeric types like numpy.float64
, are immutable.
Item assignment (using square brackets []
on the left side of an assignment, like my_object[index] = new_value
) is an operation that attempts to modify an object in place. Since immutable objects can not be changed, attempting item assignment on them results in the TypeError
.
TypeError: 'str' object does not support item assignment
This specific error occurs when you try to change a character within a string using index assignment.
my_str = 'abc'
# my_str[0] = 'z' # ⛔️ TypeError: 'str' object does not support item assignment
Solutions:
Solution: Using str.replace()
If you want to replace occurrences of a character or substring, use replace()
:
my_str = 'apple, banana, apple'
new_str = my_str.replace('apple', 'melon') # Creates a NEW string
print(new_str) # Output: melon, banana, melon
Solution: Converting to List, Modifying, and Joining
To change a character at a specific index, convert the string to a list (which is mutable), modify the list, and then join it back into a string:
my_str = 'abc'
my_list = list(my_str) # Convert to list ['a', 'b', 'c']
my_list[0] = 'z' # Modify the list
new_str = ''.join(my_list) # Join back to string
print(new_str) # Output: zbc
Solution: Using String Slicing
Construct a new string using slicing and concatenation:
my_str = 'hello_world'
idx = my_str.index('_') # Find the index to replace
new_str = my_str[:idx] + ' ' + my_str[idx+1:] # Create a new string
print(new_str) # Output: hello world
TypeError: 'int' object does not support item assignment
This error occurs when you try to treat an integer as if it were a sequence or container and assign a value to a specific "position" (which doesn't exist for integers).
my_int = 123
# my_int[0] = 9 # ⛔️ TypeError: 'int' object does not support item assignment
Solutions:
Solution: Assigning to a New Variable
If you simply wanted a different integer value, assign it to a new variable or reassign the existing one:
my_int = 123
new_int = 9
# or reassign:
# my_int = 9
print(my_int) # Output: 123
print(new_int) # Output: 9
Solution: Modifying Elements in a List/Array
If the integer was part of a mutable container (like a list) and you intended to change that element, use the container's indexing:
my_list = [1, 2, 3]
my_list[0] = 9 # Modify the element within the list
print(my_list) # Output: [9, 2, 3]
TypeError: 'numpy.float64' object does not support item assignment
(and similar NumPy types)
This error is analogous to the int
error but occurs with NumPy's specific numeric types (like np.float64
, np.int32
, etc.). These types represent single numeric values and are also immutable.
import numpy as np
my_float = np.float64(1.1)
# my_float[0] = 9.9 # ⛔️ TypeError: 'numpy.float64' object does not support item assignment
Solutions:
Solution: Assigning to a New Variable
Assign the new value to a different variable or reassign the existing one:
import numpy as np
my_float = np.float64(1.1)
another_float = np.float64(9.9)
# or reassign:
# my_float = np.float64(9.9)
print(my_float) # Output: 1.1
print(another_float) # Output: 9.9
Solution: Modifying Elements in a NumPy Array
If the NumPy float/int was an element within a NumPy array (which is mutable), modify the element using array indexing:
import numpy as np
my_arr = np.array([1.1, 2.2, 3.3], dtype=np.float64)
my_arr[0] = 9.9 # Modify the element within the array
print(my_arr) # Output: [9.9 2.2 3.3]
Checking Variable Types
If you're unsure why you're getting this error, check the type of the variable you're trying to modify:
my_var = 123.45 # Or some other value causing the error
print(type(my_var)) # Output: <class 'float'>
# Check if it's an immutable numeric/string type
print(isinstance(my_var, (str, int, float, np.number))) # Output: True
Conclusion
The TypeError: '...' object does not support item assignment
arises from attempting to modify immutable types like strings, integers, or floats (including NumPy types) using index assignment.
To resolve this, understand that you can not change these objects in place. Instead, create new objects (e.g., new strings via replace()
or slicing, new numbers via assignment) or modify elements within mutable containers like lists or NumPy arrays using their respective indexing mechanisms.