Last updated: Apr 8, 2024
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.
Copied!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.
Copied!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.
Alternatively, you can set a fallback value if the variable stores None .
Copied!my_dict = None if my_dict is None: my_dict = > my_dict['name'] = 'Bobby Hadz' print(my_dict) # ๐๏ธ
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:
Functions that don't explicitly return a value return None .
Copied!# ๐๏ธ 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.
Copied!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 .
Copied!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.
Copied!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.
Copied!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 .
In all other cases, the function doesn't return anything and ends up implicitly returning 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.
Copied!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 of whether the condition is met.
You can learn more about the related topics by checking out the following tutorials:
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.