TypeError: 'NoneType' object is not subscriptable in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
4 min

banner

# TypeError: 'NoneType' object is not subscriptable in Python

The Python "TypeError: 'NoneType' object is not subscriptable" occurs when we try to access a None value at a specific index.

To solve the error, track down where the variable got assigned None and correct the assignment to a list, tuple, dictionary or string.

typeerror nonetype object is not subscriptable

Here is an example of how the error occurs.

main.py
a_list = None # โ›”๏ธ TypeError: 'NoneType' object is not subscriptable result = a_list[0] # ๐Ÿ‘ˆ๏ธ Accessing None at an index

nonetype object is not subscriptable

We tried to access a None value at a specific index which caused the error.

And here is another example.

main.py
a_list = ['bobby', 'hadz', 'com'] result = a_list.sort() # ๐Ÿ‘ˆ๏ธ sort() returns None # โ›”๏ธ TypeError: 'NoneType' object is not subscriptable result[0]

In the second example, the error was caused because we assigned the result of calling list.sort() to a variable.

Methods like sort(), reverse(), append(), extend() mutate the original object in place and return None.

# Built-in methods that mutate the object return None

In this case, you can solve the error by removing the assignment.

main.py
a_list = ['bobby', 'hadz', 'com'] a_list.sort() # ๐Ÿ‘ˆ๏ธ sort() returns None print(a_list) # ๐Ÿ‘‰๏ธ ['bobby', 'com', 'hadz'] print(a_list[0]) # ๐Ÿ‘‰๏ธ bobby

built in methods that mutate the object return none

There is a convention in Python for methods that mutate an object in place to return None.

# Track down where the variable got assigned a None value

To solve the error in all other scenarios, you have to track down where the variable got assigned a None value and correct the assignment.

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.

# Forgetting to return a value from a function

Make sure you haven't forgotten to return a value from a function.

main.py
# ๐Ÿ‘‡๏ธ this function returns None def get_list(): print(['bobby', 'hadz', 'com']) result = get_list() print(result) # ๐Ÿ‘‰๏ธ None # โ›”๏ธ TypeError: 'NoneType' object is not subscriptable print(result[0])

forgetting to return value from function

The result variable stores a None value because all Python function that don't explicitly return a value return None.

To solve the error, use the return statement to return a value from the function.

main.py
def get_list(): return ['bobby', 'hadz', 'com'] result = get_list() print(result) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com'] print(result[0]) # bobby

# Checking if a variable doesn't store None before accessing it at an index

If you need to check if a variable doesn't store None before accessing it at a specific index, use an if statement.

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

check if variable does not store none

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

# Returning 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(['bobby', 'hadz']) # โ›”๏ธ TypeError: 'NoneType' object is not subscriptable print(my_list[0])

returning value only if condition is met

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.

# Return a default value if the condition isn't met

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 ['default value'] # ๐Ÿ‘‡๏ธ ['default value'] my_list = get_list(['bobby', 'hadz']) print(my_list[0]) # ๐Ÿ‘‰๏ธ 'default value'

return default value if condition is not met

Now the function is guaranteed to return a value regardless of whether the condition is met.

# The print() function returns None

The error also commonly occurs when storing the output of the print() function in a variable because print() returns None.

main.py
variable = print('Some message') print(variable) # ๐Ÿ‘‰๏ธ None # โ›”๏ธ TypeError: 'NoneType' object is not subscriptable print(variable[:5])

If you are trying to slice the value you are passing to the print() function, use square brackets inside of the parentheses.

main.py
print('Some message'[:4]) # ๐Ÿ‘‰๏ธ 'Some'

You can hover over a function in your IDE to see if it returns None.

The "object is not subscriptable" TypeError basically means that the object cannot be accessed using square brackets.

You should only use square brackets to access subscriptable objects.

The subscriptable objects in Python are:

  • list
  • tuple
  • dictionary
  • string

All other objects have to be converted to a subscriptable object by using the list(), tuple(), dict() or str() classes to be able to use bracket notation.

Subscriptable objects implement the __getitem__ method whereas non-subscriptable objects do not.

main.py
a_list = [1, 2, 3] # ๐Ÿ‘‡๏ธ <built-in method __getitem__ of list object at 0x7f71f3252640> print(a_list.__getitem__)
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