How to solve TypeError: ‘NoneType’ object is not subscriptable in Python

The ‘NoneType’ object is not subscriptable error in Python occurs when you try to access an element of a None object using the index. In this tutorial, we will handle the problem together by fixing common mistakes that make the error. Let’s go on.

Reason for” ‘NoneType’ object is not subscriptable” in Python

With an iterable object, you can use the square brackets [] to access its elements through the index. But if you do the same with a None object, the system will send you the error ‘NoneType’ object is not subscriptable. Look at the following example that causes the error:

Code:

# Function to create a list of integers
def createList():
    aList = []
    for i in range(10):
        aList.append(i)
    print(aList)

myList = createList()
print("The element in the index 3 is:", myList[3])  # <-- Error raises here

Result:

Traceback (most recent call last):
   line 9, in <module>
    print("The element in the index 3 is:", myList[3]) # <-- Error raises here
TypeError: 'NoneType' object is not subscriptable

Maybe you are confused that the function prints out the list of integers, but how the error still occurs. The reason is that you have a small mistake when creating the function. It is a non-returning function – a function that returns nothing. It just prints out the list. Next, we will show you how to avoid the error.

Another mistake that causes the error is applying the sort(), reverse(), append(), and extend() functions to the original list. Because after applying the above function, the assigned object will be a NoneType.

Code:

aList = [2, 5, 1, 7, 3, 6, 12]
sortList = aList.sort() # --> sortList = None
print("The smallest element is:", sortList[0])

Result:

Traceback (most recent call last):
   line 3, in <module>
    print("The smallest element is:", sortList[0])
TypeError: 'NoneType' object is not subscriptable

Solution to the error

Create a returning-function 

The function should return the list instead of printing the list to call the function; the object will be assigned to the list. Then, we can access the index correctly without raising errors. 

Code:

# Function to create a list of integers
def createList():
    aList = []
    for i in range(10):
        aList.append(i)
    return aList # <-- return instead of print

myList = createList()
print("The element in the index 3 is:", myList[3])

Result:

The element in the index 3 is: 3

Use the square brackets to access the index

The sort() function changes the order of the list directly without creating a new object. As a result, you can not assign the value to a new object while sorting the list. To solve the error, access the original list instead. 

Code:

aList = [2, 5, 1, 7, 3, 6, 12]
aList.sort()
print("The smallest element is:", aList[0])

Result:

The smallest element is: 1

Summary

In summary, the error “TypeError: ‘NoneType’ object is not subscriptable” in Python occurs when you access the index of a NoneType object that is created by a non-returning function or assigning the value to a new object while using the sort(), reverse(), append(), extend() functions. To fix the error, remember to create a returning function and access the original object after applying one of the above functions.

Leave a Reply

Your email address will not be published. Required fields are marked *