‘NoneType’ object is not subscriptable”

If you want to access none value using indexing then the TypeError: ‘NoneType’ object is not subscriptable” generates. Table of Contents What are subscriptable objects‘NoneType’ object is not subscriptable” Conclusion: What are subscriptable objects subscriptable objects …

‘NoneType’ object is not subscriptable”

If you want to access none value using indexing then the TypeError: ‘NoneType’ object is not subscriptable” generates.

What are subscriptable objects

subscriptable objects are those values that can be accessed using indexing. If you want to work with an individual part of a large collection then you can use subscript.

‘NoneType’ object is not subscriptable” 

In simple words the TypeError: ‘NoneType’ object is not subscriptable” generates when you try to index an object that does not have the functionality of indexing. Sometimes there are objects that are shown as list or dictionary but in actual they are None. For example the built-in functions of Python like sort(), reverse() and append() are used to modify the list or dictionary. As a result they will return a None value.

Example:

Let’s take an example employees information for an organization

employee = [
	   { "name": "Harry", "salary": 25000, "post": "manager" },
	   { "name": "Leena", "salary": 25000, "post": "HR" }
]

The above code contains two dictionaries. Now we ask the user to simply add the information of the employees as:

name = input("Enter the name of the employee: ")
salary = input("Enter the salary of the employee: ")
post = input("Enter the post of the employee: ")

The above code trace three things name, salary and post.

Now we append this information to the employee list by creating a new dictionary that will add to the list using append() method.

new_employee = employee.append(
	{ "name": name, "salary": salary, "post": post }
)

Now the code add the new information to the employee list and the results of append() stores in the variable new_employee.

So, when you print the result of new_employee:

print(new_employee[-1])

You will get the following output:

Output: 
NoneType’ object is not subscriptable
Solution:

This problem can be solve by removing the declaration of new_employee and place the employee.append() there

employee.append(
	{ "name": name, "salary": salary, "post": post }
)
print(employee[-1])
output:
NoneType’ object is not subscriptable

Conclusion:

We receive the TypeError message when we compute incompatible data types together. We will get the  ‘NoneType’ object is not subscriptable” whenever we try to break the values or objects that are of none type or empty data object then we will get this error.

Suggested Read: How to Change the Language on Twitter

Leave a Comment