Last updated: Apr 10, 2024
Reading timeยท4 min
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.
Here is an example of how the error occurs.
a_list = None # โ๏ธ TypeError: 'NoneType' object is not subscriptable result = a_list[0] # ๐๏ธ Accessing None at an index
We tried to access a None
value at a specific index which caused the error.
And here is another example.
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.
sort()
, reverse()
, append()
, extend()
mutate the original object in place and return None
.In this case, you can solve the error by removing the assignment.
a_list = ['bobby', 'hadz', 'com'] a_list.sort() # ๐๏ธ sort() returns None print(a_list) # ๐๏ธ ['bobby', 'com', 'hadz'] print(a_list[0]) # ๐๏ธ bobby
There is a convention in Python for methods that mutate an object in place to
return None
.
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:
None
implicitly).None
.Make sure you haven't forgotten to return a value from a function.
# ๐๏ธ 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])
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.
def get_list(): return ['bobby', 'hadz', 'com'] result = get_list() print(result) # ๐๏ธ ['bobby', 'hadz', 'com'] print(result[0]) # bobby
If you need to
check if a variable doesn't store None before
accessing it at a specific index, use an if
statement.
a_list = None if a_list is not None: print(a_list[0]) else: # ๐๏ธ this runs print('The variable stores a None value')
The if
block only runs if the variable doesn't store a None
value, otherwise
the else
block runs.
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(['bobby', 'hadz']) # โ๏ธ TypeError: 'NoneType' object is not subscriptable print(my_list[0])
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 ['default value'] # ๐๏ธ ['default value'] my_list = get_list(['bobby', 'hadz']) print(my_list[0]) # ๐๏ธ 'default value'
Now the function is guaranteed to return a value regardless of whether the condition is met.
The error also commonly occurs when storing the output of the print()
function
in a variable because print()
returns None
.
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.
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:
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.
a_list = [1, 2, 3] # ๐๏ธ <built-in method __getitem__ of list object at 0x7f71f3252640> print(a_list.__getitem__)
You can learn more about the related topics by checking out the following tutorials: