Borislav Hadzhiev
Wed Apr 20 2022·2 min read
Photo by Jordan Bauer
The Python "TypeError: 'bool' object is not subscriptable" occurs when we use
square brackets to try to access a bool
object at a specific index or specific
key. To solve the error, track down where the value got assigned a boolean and
correct the assignment.
Here are 2 examples of how the error occurs.
my_bool = True # ⛔️ TypeError: 'bool' object is not subscriptable print(my_bool[0]) # ⛔️ TypeError: 'bool' object is not subscriptable print(my_bool['name'])
Boolean objects are not subscriptable. In other words we can't use square brackets to access a boolean at a specific index or to try to access a boolean's key.
Make sure you haven't reassigned a subscriptable value to a boolean somewhere in your code.
my_list = ['a', 'b', 'c'] # 👇️ reassigned to boolean by mistake my_list = False # ⛔️ TypeError: 'bool' object is not subscriptable print(my_list[0])
We initially set the my_list
variable to a list, but then we reassigned it to
a boolean which caused the error.
You can try to remove the square brackets accessor if the value is intended to be a boolean.
The "TypeError: object is not subscriptable" means that we are using square brackets to either access a key in a specific object or to access a specific index, however the object doesn't support this functionality.