In this post, we are going to learn how to fix TypeError:NoneType object is not subscriptable in python. The Python object strings, lists, tuples, and dictionaries are subscribable because they implement the __getitem__ method. The subscript operator [] is used to access the element by index in Python from subscriptable objects. Whenever we access any element by index like list[i] from subscribable objects then internally it calls list. __getitem__[i].
1. What is TypeError:NoneType object is not subscriptable
if we access an element of none type by using subscript operator [] or assign the result of the built-in method append(), sort(), and reverse() to a variable the TypeError: ‘NoneType’ object is not subscriptable” will encounter. In case of dictionary built-in method update()
Python None type is the type that is used for the None type object. It represents no value or missing a value. For example, if a function does not return any value then returns default value will be None like append() and sort(),reverse().
1.1 typeerror:NoneType object is not subscriptable list
In this below example we have appended an element to a list and assigned value return by list. append() method to a variable and type error is raised. Even This error will be raised if we use the sort(), and the reverse() method the same way.
- As we can see, the value returned by the list.append() method is none
- Now we are trying to retrieve the value by using the subscript operator []. So we encounter with “TypeError: ‘NoneType’ object is not subscriptable”.The none type can’t be accessed by the subscript operator
mylist = [3, 6, 9, 12]
result = mylist.append(15)
print("Value return append method :", result)
print(type(result))
print("Access element by subscript operator:", result[0])
Output
Value return append method: None
<class 'NoneType'>
"print("Access element by subscript operator:", result[0])"
TypeError: 'NoneType' object is not subscriptable
- As we can see, the value returned by the list.append() method is none
- Now we are trying to retrieve the value by using the subscript operator []. So we encounter with “TypeError: ‘NoneType’ object is not subscriptable”.The none type can’t be accessed by the subscript operator
Using list.sort() method
mylist = [3, 6, 9, 12]
result = mylist.sort()
print("Value return append method :", result)
print(type(result))
print("Access element by subscript operator:", result[0])
Output
Value return append method: None
<class 'NoneType'>
"print("Access element by subscript operator:", result[0])"
TypeError: 'NoneType' object is not subscriptable
1.2.typeerror ‘nonetype’ object is not subscriptable dictionary
In this python example, we are updating the dictionary and accessing the value return by the update() method to variable and accessing it by using the update_dict[0] subscript operator. So the type error is raised.
mydict = {'math':100,'Eng':100,'Chem':98}
key_vals = {'Aon':200}
update_dict = mydict.update(key_vals)
print(update_dict)
print(update_dict[0])
Output
print(update_dict[0])
TypeError: 'NoneType' object is not subscriptable
2. How to solve TypeError: ‘NoneType’ object is not subscriptable list
The solution to this error is to use the sort(), append(), and reverse method in place instead of accessing it to a variable and accessing the element by the subscript operator.
In this example, we have updated the list in place without access to a variable and accessed the list element by using the subscript operator[].
mylist = [3, 6, 9, 12]
mylist.append(15)
print('updated list',mylist)
print("Access element by subscript operator:", mylist[0])
Output
updated list [3, 6, 9, 12, 15]
Access element by subscript operator: 3
3. How to solve TypeError: ‘NoneType’ object is not subscriptable dictionary
The solution to this error is to use the sort(), append(), and reverse method in place instead of accessing it to a variable and accessing the element by the subscript operator.
In this example, we have updated the dictionary in place using the update() method without access to a variable and accessed the dictionary element by using the subscript operator bypassing the key name.
mydict = {'math':100,'Eng':100,'Chem':98}
key_vals = {'Aon':200}
mydict.update(key_vals)
print(mydict)
print(mydict['math'])
Output
{'math': 100, 'Eng': 100, 'Chem': 98, 'Aon': 200}
100
Summary
In this post, we have learned how to solve TypeError:NoneType object is not subscriptable in python with examples. This type of error occurs when we assign the result of the built-in method append(), sort(), and reverse() to a variable and access them using the subscript operator.