Typeerror: ‘nonetype’ object is not subscriptable (Fixed)

Python is a very popular programming language most often used for developing the backend of websites, for data analysis, machine learning, or automation tasks. Python is an object-oriented language, meaning everything within Python is an object, and these objects can contain information. Editing, testing, and debugging are fast and easy in Python because of the exceptions raised by the interpreter. If you are seeing the error, Typeerror: ‘nonetype’ object is not subscriptable when you try to execute your code, don’t worry. This article will walk you through the breakdown of this error. 

Below you will find examples of where this error will show up and how to prevent and fix this Typeerror. 

Typeerror: ‘nonetype’ object is not subscriptable error message in Python console

What is a Typeerror? 

In Python, TypeError is an error generated when you try to perform an operation on the wrong type of object. Python is an object-oriented language, and each object is assigned specific functions. Different objects types are used for different purposes. Basic object types include:

  • Numbers: integers, complex numbers, and floating-point numbers
  • Strings: store written characters or words
  • Lists: store a sequence of objects that can be numbers, strings, etc
  • Booleans: True or False 
  • Null: lack of value

Python checks what type of object you intend to pass through before completing an operation. If the object type does not support the operation, a TypeError is thrown. For example, a number type can use mathematical operations like the summing of two numbers, the quotient of two numbers, or the absolute value of a number, to name a few.

What is a subscriptable object?

Indexing and subscripting are essentially the same. Strings, lists, tuples, and dictionaries fall into the subscriptable category in Python. Objects that are subscriptable contain or can contain other objects. 

The [item] method can be used with square brackets. For example, you would use list[1] to index a list.

Subscriptable objects use the __getitem__() method. This method allows Python to retrieve a specific object from a collection of objects. Subscriptable objects will record the operations that are done to them.

Now, the problem arises when you attempt to use square brackets to call a method inside a class. In such cases, curly brackets should be used instead. An error announcing the object is not subscriptable will be thrown when objects that do not contain other objects are attempted to be indexed. In such circumstances, the ‘method’ object is not subscriptable error occurs.

What is a ‘Nonetype’ object?

Null is a special value, or variable, with no value. The variable is empty and stores no data. You cannot reassign null objects to any other value. They are always null. 

In Python, None is a complete object. It belongs to the data type of the class NoneType. None can be assigned to any variable, but all variables that are assigned to None point to the same object, a valueless object. You can only use None objects with a few operators, including:

  • is
  • ==

Is None used in other languages? 

None is represented differently in different languages. None is more commonly referred to as null. As such, null is confusing and can be difficult to remember when learning new languages. Null is used in other languages through the values:

  • Javascript uses the null value
  • Ruby uses the nil value and the NilClass type
  • Swift uses nil
  • PHP uses the value null

Where is None used?

NoneType is also a common default return value for functions that search for something and may or may not find it. When using type(None) you will get the return <class ‘NoneType’ > because none is an object, and its type is NoneType.

NoneType can be used to compare or match two expressions. If the match is found, you’ll return a match object (when using re.match()), but if not, you’ll return a none object. This is not the same as false. 

When Nonetype appears in traceback, it means that something you didn’t expect to be None actually was None, and you tried to use it in a way you can’t. 

Examples and solutions for Typeerror: ‘nonetype’ object is not subscriptable

The basics

First, let’s look at a simple way to understand this error better. All you need to do to make a non-subscriptable item subscriptable is wrap the object in a container data type:

x = None

This code is asking for the object at position 0 in x. But x is not a list, and the value None is not subscriptable, so this code will throw the error:

Traceback (most recent call last):
  File "c:\User\Python\main.py", line 9, in <module>
TypeError: 'NoneType' object is not subscriptable

Changing the code above by wrapping the object in brackets will solve this problem:

x = [None]

Will yield the output:


Example 1: Assigning methods to variables

The TypeError: ‘nonetype’ object is not subscriptable error commonly rises when the result of methods like sort(), reverse(), and append() are assigned to a variable. These methods will return a None value when assigned this way. The example below demonstrates this problem:

values = [1, 2, 3, 4, 5 ,6 , 7]
valuesreverse = values.reverse()

This bit of code asks the method reverse() to be applied to the list named values. If this method is executed as expected, print(valuesreverse[0]) will print the number 7 when calling the first object at position 0 of the list. However, the output of this code is printed below:

Traceback (most recent call last):
  File "c:\Username\Python\main.py", line 9, in <module>
TypeError: 'NoneType' object is not subscriptable

To prevent this error, you should not assign the reverse() method (or any method) to a variable. Instead, the method should act upon the variable. The correct way to use the reverse() method for this list:

values = [1, 2, 3, 4, 5 ,6 , 7]
result = values[0]
print(‘Value at position 0: ’, result)

In this code snippet, the reverse method acts on the values list. At position 0 in the list values, the object is assigned to a variable called result. When this code runs, the output is:

Value at position 0: 7

Example 2: Real-life Example

Let’s look at another example to further your understanding. This time you will attempt a more realistic instance. You are going to create a program that tracks multiple pieces of information for employees. First, you need to define your list of employees:

employees = [ 
{‘first_name’: ‘John’, ‘last_name’: ‘Smith’, ‘age’: 35}
{‘first_name’: ‘Susan’, ‘last_name’: ‘Lee’, ‘age’: 29}

From this, you can see that the employee list has two entries or dictionaries. Now you are going to create variables that prompt you to input new employee data:

first_name = input(‘Employee’s first name: ’)
last_name = input(‘Employee’s last name: ’)
age = input(‘Employee’s age: ‘)

To collect and add a new employee to the employee list, you may be tempted to try this:

new_employee = employee.append(
	{ ‘first_name’: Joe, ‘last_name’: Bob, ‘age’: 48}

So you appended the employee list and are trying to print out the last entry in the list, but the code returns:

Traceback (most recent call last):
  File "c:\Username\Python\main.py", line 14, in <module>
TypeError: 'NoneType' object is not subscriptable

Can you spot the problem? 

You tried to assign the method append() to a new variable. To fix this problem simply:

	{ ‘first_name’: Joe, ‘last_name’: Bob, ‘age’: 48}

This syntax works because you add the new information to the existing list. When you append() a list and assign it to a variable, that list becomes a None object, and you will not be able to subscript it.

Summarizing Typeerror: ‘nonetype’ object is not subscriptable

In Python, when you read the error Typeerror: ‘nonetype’ object is not subscriptable in your interpreter, you are probably using the wrong operator on a non-subscriptable Nonetype object. You need to either turn that object into a list, or if the object is already a list make sure you are not applying the method to the object while assigning it to a variable. 

Understanding Typeerror, ‘nonetype’ objects, and subscriptable will be useful when creating projects in Python. You will need these concepts for projects containing lists, dictionaries, and databases.

Leave a Comment