Python TypeError: ‘NoneType’ object is not subscriptable Solution

Posted in /  

Python TypeError: ‘NoneType’ object is not subscriptable Solution
vinaykhatri

Vinay Khatri
Last updated on December 3, 2024

    The None value in Python has a data type of NoneType, that represents a Falsy value.  We generally use the None value to initialize a variable or identifier, which value we want to change throughout the program. If we perform an indexing operation on a NoneType value, we receive the error “ TypeError: ‘NoneType’ object is not subscriptable ”.

    In this Python guide, we will walk through this Python error and discuss how to debug it. We will also discuss a typical example scenario that demonstrates the error and its solution. So let’s get started with the error statement.

    Python Problem TypeError: 'NoneType' object is not subscriptable

    In Python, some data types support subscriptable operation on their data object. For instance, we can access the individual item or character from a string, dictionary, list, and tuple using the square bracket notation. But if we try to perform the same operation on a NoneType object “None”, we encounter the error “ TypeError: ‘NoneType’ object is not subscriptable ” Error.  This error statement has two parts Exception Type and Error Message.

    1. TypeError (Exception Type)
    2. ‘NoneType’ object is not subscriptable (Error Message)

    1. TypeError

    TypeError is a standard Python exception, and Python raises this exception in a program when we try to perform an invalid operation or function on an unsupported data type. Here particularly, we are getting this exception because the None object does not support the subscriptable operation.

    2. ‘NoneType’ object is not subscriptable

    NoneType object is not subscriptable is the error message that raises along with the TypeError exception. This error message notifies us that we are trying to perform the indexing or subscriptable operation on a Python’s NoneType value, i.e., None. And Python does not support that operation on a None value.

    Example

    #initialize a None value
    x = None
    
    #perform the indexing operation on the None value
    print(x[0])

    Output

    Traceback (most recent call last):
      File "main.py", line 5, in <module>
        print(x[0])
    TypeError: 'NoneType' object is not subscriptable

    As you can see, we received the error when we tried to perform the square bracket indexing notation on a None value x.

    Common Example Scenario

    Now we know that a None value is not subscriptable, which means it does not support indexing. The example that we gave in the above section was pretty minimal and straightforward. If you already know the identifier value is None, you won’t perform indexing on that specific value. In most cases, you will encounter this error when you assign a list, tuple, string, or dictionary value with a None returning function or method.

    Error Example

    Suppose we have a list of programming tutorials, and we want to sort those tutorials in alphabetical order. For that, we can use the list’s inbuilt sort() method.

    tutorials = ["C", "Python", "Java", "C++", "JavaScript"]
    
    # sort the tutorial
    tutorials = tutorials.sort()
    
    #access the first tutorial 
    print(tutorials[0])

    Output

    Traceback (most recent call last):
      File "main.py", line 7, in <module>
        print(tutorials[0])
    TypeError: 'NoneType' object is not subscriptable

    Break the output

    In the above example, we are receiving the error in line 7 with print(tutorials[0]) .  In line 7, the value of tutorials is None because, in line 4, we are reassigning the sort() method return value to the tutorials list using the “ tutorials = tutorials.sort() ”. The sort() method performs the inline sorting and returns None as a value. And when we assigned it to the tutorials , the value of the tutorials list became None . And we know that we get the TypeError: 'NoneType' object is not subscriptable error when performing the indexing operation on a None value.

    Solution

    When we function or method return value to an identifier, we should know the return value or type of the function. In the above example, we assigned the return value of the sort() method to the tutorials list, making the tutorials value to None. To solve the above problem, we need to ensure that we are not assigning any None value to the tutorials list.

    tutorials = ["C", "Python", "Java", "C++", "JavaScript"]
    
    # sort the tutorial
    tutorials.sort()
    
    #access the first tutorial 
    print(tutorials[0])

    Output

    C

    Now our code runs successfully.

    Conclusion

    The “TypeError: 'NoneType' object is not subscriptable ” is a common error that many Python learners encounter when they are accidentally accessing a list, string, tuple, or dictionary value to None and perform an indexing operation on it. This error is only raised in a Python program when we try to perform the subscriptable square bracket indexing notation on a None value.  If your Python program is raising this error, you can share your code in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments