What This Error Means and Why It Happens

The "TypeError: 'NoneType' object is not subscriptable" error occurs when your code tries to use square brackets to access an item inside something that is None instead of a list, dictionary, or string. None is Python's way of saying "nothing is here" — it is not an empty list or an empty dictionary, it is the absence of a value altogether.

When Python sees code like my_variable[0] or my_variable['key'], it expects my_variable to hold a collection of items. If my_variable is None, Python cannot subscript it (cannot reach inside it with brackets), so it stops and throws this error.

The most common cause is a function that returns None when you expected it to return a list or dictionary. A second common cause is a variable that was set to None earlier in your code, or never set to anything at all. A third is trying to subscript the result of a method that failed silently and returned None instead of raising an error.

Key Takeaways

  • This error means you are trying to use square brackets on a variable that contains None, not a list, dictionary, or string.
  • The fix usually involves checking whether a variable is None before you try to subscript it, or ensuring the function that created it actually returns data.
  • Print or log the variable before you subscript it to see whether it is None or what it actually contains.
  • Use an if statement to check if the variable is not None before you try to access items inside it.

Identify Where the Error Occurs

The error message will tell you the line number where the subscript attempt happens. Look at that line and find the variable name followed by square brackets. That variable is None.

For example, if the error says the problem is on line 12 and line 12 reads result = data[0], then data is None. If line 12 reads value = response['status'], then response is None.

Once you know which variable is None, trace backward to see where it came from. Did a function return it? Was it assigned directly? Did you forget to assign it at all?

Check Whether a Function Is Returning None

If the None variable came from a function call, the function itself may be returning None when it should return a list or dictionary. This often happens when a function has multiple code paths and one of them does not explicitly return anything.

In Python, a function that reaches the end without a return statement automatically returns None. For example:

def get_items():   if some_condition:     return [1, 2, 3]   # If some_condition is False, the function returns None

To fix this, make sure every code path in the function returns something. Add a return statement at the end that returns an empty list or dictionary if no other condition was met, or return a default value that makes sense for your program.

Add a Check Before You Subscript

The safest fix is to check whether the variable is None before you try to subscript it. Use an if statement:

if data is not None:   result = data[0] else:   result = None # or handle the None case differently

This prevents the error and lets you decide what to do when the variable is None. You can set a default value, skip that step, log a message, or raise a more informative error.

If you are working with a dictionary, you can also use the get() method instead of square brackets. The get() method returns None (or a default value you specify) if the key does not exist, instead of raising an error:

value = response.get('status') # Returns None if 'status' key does not exist

Print or Log the Variable to See What It Contains

Before you fix the code, add a print statement or log message right before the line that fails. This shows you what the variable actually contains:

print(f"data is: {data}") # Add this line result = data[0] # This is where the error happens

When you run the code, the print statement will show you whether data is None, an empty list, or something else entirely. This tells you whether the problem is that the variable is None, or whether it is something else (like a string when you expected a list).

Handle None Values Gracefully in Your Program

Once you know where None is coming from, decide whether it should be there. If a function sometimes returns None and sometimes returns data, your code should handle both cases. Use if statements, try-except blocks, or default values to make sure your program does not crash when it encounters None.

If None should never happen, add an assertion or raise an error with a clear message. This makes bugs easier to find:

assert data is not None, "data should not be None at this point" result = data[0]

This way, if data is None when it should not be, you get a clear error message that tells you something went wrong earlier in your program, not just "NoneType object is not subscriptable."

Frequently Asked Questions

Why does my function return None when I did not write a return statement?

In Python, every function returns something. If you do not write a return statement, the function automatically returns None. To return a list or dictionary, you must write return followed by the value you want to return. If a function has multiple paths, make sure every path has a return statement.

Can I use square brackets on a string that is None?

No. None is not a string, list, or dictionary — it is the absence of a value. You cannot subscript None no matter what type you expected. You must check whether the variable is None before you try to subscript it.

What is the difference between None and an empty list?

None means "no value." An empty list is [] — it is a real list with zero items. You can subscript an empty list (you will just get an index error if you ask for an item that does not exist), but you cannot subscript None. If a function should return a list, return [] instead of None when there is no data.

Does the get() method work on lists?

No, get() only works on dictionaries. For lists, use an if statement to check the length before you subscript: if len(data) > 0: result = data[0]. Or use try-except to catch the IndexError if the list is too short.