Python objects with the value None cannot be accessed using indexing. This is because None values do not contain data with index numbers.
If you try to access an item from a None value using indexing, you encounter a “TypeError: ‘NoneType’ object is not subscriptable” error.
In this guide, we talk about what this error means and break down how it works. We walk through an example of this error so you can figure out how to solve it in your program.
TypeError: ‘NoneType’ object is not subscriptable
Subscriptable objects are values accessed using indexing. “Indexing” is another word to say “subscript”, which refers to working with individual parts of a larger collection.
For instance, lists, tuples, and dictionaries are all subscriptable objects. You can retrieve items from these objects using indexing. None values are not subscriptable because they are not part of any larger set of values.
The “TypeError: ‘NoneType’ object is not subscriptable” error is common if you assign the result of a built-in list method like sort()
, reverse()
, or append()
to a variable. This is because these list methods change an existing list in-place. As a result, they return a None value.
An Example Scenario
Build an application that tracks information about a student’s test scores at school. We begin by defining a list of student test scores:
scores = [ { "name": "Tom", "score": 72, "grade": "B" }, { "name": "Lindsay", "score": 79, "grade": "A" } ]
Our list of student test scores contains two dictionaries. Next, we ask the user to insert information that should be added to the “scores” list:
name = input("Enter the name of the student: ") score = input("Enter the test score the student earned: ") grade = input("Enter the grade the student earned: ")
We track three pieces of data: the name of a student, their test score, and their test score represented as a letter grade.
Next, we append this information to our “scores” list. We do this by creating a dictionary which we will add to the list using the append() method:
new_scores = scores.append( { "name": name, "score": score, "grade": grade } )
This code adds a new record to the “scores” list. The result of the append()
method is assigned to the variable “new_scores”.
Finally, print out the last item in our “new_scores” list so we can see if it worked:
print(new_scores[-1])
The value -1 represents the last item in the list. Run our code and see what happens:
Enter the name of the student: Wendell Enter the test score the student earned: 64 Enter the grade the student earned: C Traceback (most recent call last): File "main.py", line 14, in <module> print(new_scores[-1]) TypeError: 'NoneType' object is not subscriptable
Our code returns an error.
The Solution
Our code successfully asks our user to insert information about a student. Our code then adds a record to the “new_scores” list. The problem is when we try to access an item from the “new_scores” list.
Our code does not work because append()
returns None. This means we’re assigning a None value to “new_scores”. append()
returns None because it adds an item to an existing list. The append()
method does not create a new list.
To solve this error, we must remove the declaration of the “new_scores” variable and leave scores.append()
on its own line:
scores.append( { "name": name, "score": score, "grade": grade } ) print(scores[-1])
We now only reference the “scores” variable. Let’s see what happens when we execute our program:
Enter the name of the student: Wendell Enter the test score the student earned: 64 Enter the grade the student earned: C {'name': 'Wendell', 'score': '64', 'grade': 'C'}
Our code runs successfully. First, our user is asked to insert information about a student’s test scores. Then, we add that information to the “scores” dictionary. Our code prints out the new dictionary value that has been added to our list so we know our code has worked.
Conclusion
The “TypeError: ‘NoneType’ object is not subscriptable” error is raised when you try to access items from a None value using indexing.
This is common if you use a built-in method to manipulate a list and assign the result of that method to a variable. Built-in methods return a None value which cannot be manipulated using the indexing syntax.
Now you’re ready to solve this common Python error like an expert.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.