This error means you tried to use square brackets on something that doesn't exist
A TypeError: 'NoneType' object is not subscriptable error happens when your code tries to access an item using square brackets — like my_variable[0] — but my_variable is actually None instead of a list, dictionary, or string. In Python, None is a placeholder that means "nothing" or "no value." You cannot use square brackets on nothing.
The word "subscriptable" means the object supports indexing with square brackets. Lists, dictionaries, strings, and tuples are subscriptable. None is not. When Python tries to subscript None, it stops and throws this error.
Key Takeaways
- The error occurs when you use square brackets on a variable that contains None instead of a list, dictionary, or string.
- The most common cause is a function that returns None when you expected it to return data.
- Check what your variable actually contains by printing it or using a debugger before you try to subscript it.
- Add a check for None before using square brackets, or make sure your function returns the right type of object.
The most common reason: a function returned None instead of data
In most cases, this error happens because a function gave you None when you thought it would give you a list or dictionary. For example, if you call a function that searches for something and finds nothing, it might return None. Then when you try to access the first item with [0], Python crashes.
Here is a straightforward example. Say you have a function that looks up a user in a database:
def find_user(user_id): if user_id == 1: return {"name": "Alice", "age": 30} # If user not found, the function returns None implicitly result = find_user(999) # User 999 doesn't exist print(result["name"]) # ERROR: NoneType object is not subscriptable
The function did not find user 999, so it returned None. When you tried to access result["name"], Python threw the error because you cannot subscript None.
How to check what your variable actually contains
Before you use square brackets, print the variable to see what it really is. Add a line like print(my_variable) right before the line that causes the error. If it prints None, you have found the problem.
You can also check the type: print(type(my_variable)) will tell you whether it is a list, dict, string, or NoneType. This tells you exactly what went wrong and where to look next.
In a larger program, use a debugger. Most code editors let you set a breakpoint (a pause point) and step through your code line by line, watching what each variable contains. This is faster than printing dozens of times.
Add a None check before using square brackets
The safest fix is to check whether the variable is None before you try to subscript it. Use an if statement:
result = find_user(999) if result is not None: print(result["name"]) else: print("User not found")
Now if result is None, your code does not try to subscript it. Instead, it runs the else block and handles the missing data gracefully.
You can also use a short form called a ternary operator: print(result["name"] if result is not None else "User not found"). This does the same thing in one line.
Make sure your function returns the right type
If you wrote the function yourself, check what it returns. If it should return a list or dictionary but sometimes returns None, that is the root cause. Either make it always return the right type, or document that it can return None so the caller knows to check for it.
For example, rewrite the function above to always return a dictionary, even if it is empty:
def find_user(user_id): if user_id == 1: return {"name": "Alice", "age": 30} return {} # Return empty dict instead of None result = find_user(999) print(result.get("name", "User not found")) # Prints "User not found"
Now the function always returns a dictionary. The caller can use .get() to safely retrieve a key, and it will return a default value if the key does not exist.
Other places this error can hide
Sometimes the problem is not obvious. If you are working with a list of items and one of them is None, you might not notice until you try to subscript it. For example:
items = [{"id": 1}, None, {"id": 3}] for item in items: print(item["id"]) # ERROR on the second loop when item is None
Add a check inside the loop: if item is not None: print(item["id"]).
Another place to watch: if you are unpacking values from a function or API call, make sure you know which ones can be None. Read the documentation or test the function with different inputs to see what it returns.
Frequently Asked Questions
What does "subscriptable" mean?
Subscriptable means the object supports square bracket notation to access items by index or key. Lists, dictionaries, strings, and tuples are subscriptable. None, integers, and booleans are not.
Can I use square brackets on a string?
Yes. Strings are subscriptable. "hello"[0] returns "h". But if your variable is None instead of a string, the error will occur.
How do I know if a function will return None?
Read the function's documentation or look at its code. If it has a path that does not explicitly return a value, it returns None. Test it with inputs that should fail to see what happens.
Is there a way to make Python ignore this error?
You could wrap the line in a try-except block, but that hides the problem instead of fixing it. It is better to check for None first so you understand what went wrong and handle it intentionally.
Why does Python use None instead of raising an error when ready?
Functions return None by default if they do not explicitly return anything. This is a design choice that lets functions signal "no result" without crashing. Your code must check for None before using the result.