In Python, NoneType is the type for the None object, which is an object that indicates no value. Functions that do not return anything return None, for example, append()
and sort()
. You cannot retrieve items from a None value using the subscript operator []
like you can with a list or a tuple. If you try to use the subscript operator on a None value, you will raise the TypeError: NoneType object is not subscriptable.
To solve this error, ensure that when using a function that returns None
, you do not assign its return value to a variable with the same name as a subscriptable object that you will use in the program.
This tutorial will go through the error in detail and how to solve it with code examples.
Table of contents
TypeError: ‘NoneType’ object is not subscriptable
Let’s break up the error message to understand what the error means. TypeError occurs whenever you attempt to use an illegal operation for a specific data type.
The subscript operator retrieves items from subscriptable objects. The operator in fact calls the __getitem__
method, for example a[i]
is equivalent to a.__getitem__(i)
.
All subscriptable objects have a __getitem__
method. NoneType objects do not contain items and do not have a __getitem__
method. We can verify that None objects do not have the __getitem__
method by passing None
to the dir()
function:
print(dir(None))
['__bool__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
If we try to subscript a None value, we will raise the TypeError: ‘NoneType’ object is not subscriptable.
Example #1: Appending to a List
Let’s look at an example where we want to append an integer to a list of integers.
lst = [1, 2, 3, 4, 5, 6, 7] lst = lst.append(8) print(f'First element in list: {lst[0]}')
In the above code, we assign the result of the append call to the variable name lst
. Let’s run the code to see what happens:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [1], in <cell line: 5>() 1 lst = [1, 2, 3, 4, 5, 6, 7] 3 lst = lst.append(8) ----> 5 print(f'First element in list: {lst[0]}') TypeError: 'NoneType' object is not subscriptable
We throw the TypeError because we replaced the list with a None value. We can verify this by using the type() method.
lst = [1, 2, 3, 4, 5, 6, 7] lst = lst.append(8) print(type(lst))
<class 'NoneType'>
When we tried to get the first element in the list, we are trying to get the first element in the None object, which is not subscriptable.
Solution
Because append()
is an in-place operation, we do not need to assign the result to a variable. Let’s look at the revised code:
lst = [1, 2, 3, 4, 5, 6, 7] lst.append(8) print(f'First element in list: {lst[0]}')
Let’s run the code to get the result:
First element in list: 1
We successfully retrieved the first item in the list after appending a value to it.
Example #2: Sorting a List
Let’s look at an example where we want to sort a list of integers.
numbers = [10, 1, 8, 3, 5, 4, 20, 0] numbers = numbers.sort() print(f'Largest number in list is {numbers[-1]}')
In the above code, we assign the result of the sort() call to the variable name numbers. Let’s run the code to see what happens:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [8], in <cell line: 3>() 1 numbers = [10, 1, 8, 3, 5, 4, 20, 0] 2 numbers = numbers.sort() ----> 3 print(f'Largest number in list is {numbers[-1]}') TypeError: 'NoneType' object is not subscriptable
We throw the TypeError because we replaced the list numbers
with a None
value. We can verify this by using the type()
method.
numbers = [10, 1, 8, 3, 5, 4, 20, 0] numbers = numbers.sort() print(type(numbers))
<class 'NoneType'>
When we tried to get the last element of the sorted list, we are trying to get the last element in the None object, which is not subscriptable.
Solution
Because sort()
is an in-place operation, we do not need to assign the result to a variable. Let’s look at the revised code:
numbers = [10, 1, 8, 3, 5, 4, 20, 0] numbers.sort() print(f'Largest number in list is {numbers[-1]}')
Let’s run the code to get the result:
Largest number in list is 20
We successfully sorted the list and retrieved the last value of the list.
Summary
Congratulations on reading to the end of this tutorial! The TypeError: ‘NoneType’ object is not subscriptable occurs when you try to retrieve items from a None value using indexing. If you are using in-place operations like append, insert, and sort, you do not need to assign the result to a variable.
For further reading on TypeErrors, go to the articles:
- How to Solve Python TypeError: ‘function’ object is not subscriptable
- How to Solve Python TypeError: ‘bool’ object is not subscriptable
Go to the online courses page on Python to learn more about Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.