TypeError: NoneType object does not support item assignment

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# TypeError: NoneType object does not support item assignment

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.

typeerror nonetype object does not support item assignment

Here is an example of how the error occurs.

main.py
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.

# Checking if the variable doesn't store None

Use an if statement if you need to check if a variable doesn't store a None value before the assignment.

main.py
my_list = None if my_list is not None: my_list[0] = 'z' else: # ๐Ÿ‘‡๏ธ this runs print('variable stores a None value')

check if variable does not store none

The if block is only run if the variable doesn't store a None value, otherwise, the else block runs.

# Setting a fallback value if the variable stores None

Alternatively, you can set a fallback value if the variable stores None.

main.py
my_dict = None if my_dict is None: my_dict = {} my_dict['name'] = 'Bobby Hadz' print(my_dict) # ๐Ÿ‘‰๏ธ {'name': 'Bobby Hadz'}

setting fallback value if the variable stores none

If the variable stores a None value, we set it to an empty dictionary.

# Track down where the variable got assigned a None value

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:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if a certain condition is met.

# Functions that don't return a value return None

Functions that don't explicitly return a value return None.

main.py
# ๐Ÿ‘‡๏ธ 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'

functions that dont return value return none

You can use the return statement to return a value from a function.

main.py
def get_list(): return ['a', 'b', 'c'] # ๐Ÿ‘‡๏ธ ['a', 'b', 'c'] my_list = get_list() my_list[0] = 'z' print(my_list) # ๐Ÿ‘‰๏ธ ['z', 'b', 'c']

use return statement to return value

The function now returns a list, so we can safely change the value of a list element using square brackets.

# Many built-in functions return None

Note that there are many built-in functions (e.g. sort()) that mutate the original object in place and return None.

main.py
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.

main.py
a_list = ['c', 'b', 'a'] a_list.sort() a_list[0] = 'Z' print(a_list) # ๐Ÿ‘‰๏ธ ['Z', 'b', 'c']

# A function that returns a value only if a condition is met

Another common cause of the error is having a function that returns a value only if a condition is met.

main.py
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.

main.py
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.

Want to learn more about working with None in Python? Check out these resources: How to Remove the None values from a Dictionary in Python,How to Remove the None values from a List in Python.

# Additional Resources

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.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev