Last updated: Feb 1, 2023
Reading timeยท3 min
The Python "TypeError: NoneType object does not support item assignment"
occurs when we try to perform an item assignment on a None
value.
To solve the error, figure out where the variable got assigned a None
value
and correct the assignment.
Here is an example of how the error occurs.
my_list = None # โ๏ธ TypeError: 'NoneType' object does not support item assignment my_list[0] = 'a'
We tried to assign a value to a variable that stores None
.
Use an if
statement if you need to
check if a variable doesn't store a None value
before the assignment.
my_list = None if my_list is not None: my_list[0] = 'z' else: # ๐๏ธ this runs print('variable stores a None value')
The if
block is only run if the variable doesn't store a None
value,
otherwise, the else
block runs.
None
Alternatively, you can set a fallback value if the variable stores None
.
my_dict = None if my_dict is None: my_dict = {} my_dict['name'] = 'Bobby Hadz' print(my_dict) # ๐๏ธ {'name': 'Bobby Hadz'}
If the variable stores a None
value, we set it to an empty dictionary.
You have to figure out where the variable got assigned a None
value in your
code and correct the assignment to a list or a dictionary.
The most common sources of None
values are:
None
implicitly).None
.None
Functions that don't explicitly return a value return None
.
# ๐๏ธ this function returns None def get_list(): print(['a', 'b', 'c']) # ๐๏ธ None my_list = get_list() # โ๏ธ TypeError: 'NoneType' object does not support item assignment my_list[0] = 'z'
You can use the return statement to return a value from a function.
def get_list(): return ['a', 'b', 'c'] # ๐๏ธ ['a', 'b', 'c'] my_list = get_list() my_list[0] = 'z' print(my_list) # ๐๏ธ ['z', 'b', 'c']
The function now returns a list, so we can safely change the value of a list element using square brackets.
Note that there are many built-in functions (e.g. sort()
) that mutate the
original object in place and return None
.
a_list = ['c', 'b', 'a'] result = a_list.sort() print(result) # ๐๏ธ None # โ๏ธ TypeError: 'NoneType' object does not support item assignment result[0] = 'Z'
The sort()
method mutates the list in place and returns None
, so we
shouldn't store the result of calling it into a variable.
To solve the error, remove the assignment.
a_list = ['c', 'b', 'a'] a_list.sort() a_list[0] = 'Z' print(a_list) # ๐๏ธ ['Z', 'b', 'c']
Another common cause of the error is having a function that returns a value only if a condition is met.
def get_list(a): if len(a) > 3: return a # ๐๏ธ None my_list = get_list(['a', 'b'])
The if
statement in the get_list
function is only run if the passed-in
argument has a length greater than 3
.
None
.To solve the error, you either have to check if the function didn't return
None
or return a default value if the condition is not met.
def get_list(a): if len(a) > 3: return a return [] # ๐๏ธ return an empty list if condition not met # ๐๏ธ [] my_list = get_list(['a', 'b'])
Now the function is guaranteed to return a value regardless if the condition is met.
You can learn more about the related topics by checking out the following tutorials: