Skip to main content

How to Solve "TypeError: 'bool' object is not subscriptable" in Python

The TypeError: 'bool' object is not subscriptable error in Python occurs when you try to use square bracket indexing ([]) on a boolean value (True or False). Boolean values, representing simple truth or falsehood, do not support indexing.

This guide explains why this error happens and how to fix it, focusing on identifying the incorrect variable assignments that lead to the problem.

Understanding Subscriptability and Booleans

In Python, "subscriptable" means you can access elements of an object using square brackets with an index or key:

  • Lists: my_list[2] (access element at index 2)
  • Tuples: my_tuple[0] (access element at index 0)
  • Strings: my_string[5] (access character at index 5)
  • Dictionaries: my_dict['key'] (access value associated with 'key')

Boolean values (True and False) are not subscriptable. They represent a single truth value, not a collection of items. There's no concept of an "index" or "key" within a boolean.

my_bool = True

# ⛔️ TypeError: 'bool' object is not subscriptable
#print(my_bool[0]) #Trying to access my_bool as subscriptable

# ⛔️ TypeError: 'bool' object is not subscriptable
#print(my_bool['name']) #Trying to access my_bool as subscriptable

Common Causes and Solutions

The TypeError: 'bool' object is not subscriptable error always means you're treating a boolean value as if it were a list, tuple, string, or dictionary. Here are the most common causes:

Incorrect Variable Assignment

The most frequent cause is accidentally assigning a boolean value to a variable that you intend to be a list, tuple, string, or dictionary:

my_list = ['a', 'b', 'c']

# ... some other code that unexpectedly changes my_list ...
my_list = len(my_list) > 5 # Example of operation that returns boolean

# ⛔️ TypeError: 'bool' object is not subscriptable
# print(my_list[0]) # my_list is now False (or True), not a list!
  • The code initially assigns a list to my_list, but then proceeds to reassign it to a boolean.

Solution: Carefully check your code for any assignments that might change the variable's type to a boolean. Print the variable's type and value immediately before the line that causes the error to pinpoint the problem:

my_list = ['a', 'b', 'c']
my_list = len(my_list) > 5 # Accidental boolean assignment
print(type(my_list)) # Output: <class 'bool'> (This is the problem!)
print(my_list) # Output: False

Incorrect Indexing of a 2D list or array.

If the list is 2D, using only one index will return a row, and not an element.

my_list = [[1, 2, 3], [4, 5, 6]]
print(my_list[0]) # Output: [1, 2, 3]
# print(my_list[0][0]) # ⛔️ TypeError
  • If the index used in the first square brackets is not correct, it might return a boolean, instead of a row, which causes an error.

Solution: If your variable is supposed to be a two-dimensional array, make sure it is declared as such.

my_list = [[1, 2, 3], [4, 5, 6]] # Correctly declared 2D list
print(my_list[0][0]) # Output: 1

Debugging Strategies

  1. print(type(your_variable)): Use print(type(your_variable)) immediately before the line that causes the error. This will confirm if the variable is a boolean when you expect it to be a list, tuple, string, or dictionary.
  2. print(your_variable): Print the value of the variable. This can help you see if it's True or False when you expect a different value.
  3. Trace Backwards: Examine the code leading up to the error. Where is your_variable assigned? Are there any conditional statements, function calls, or operations that might be changing its type to a boolean?
  4. Use an IDE debugger: Use a debugger to step through your code and watch the variable's value and type change.

Conclusion

The TypeError: 'bool' object is not subscriptable error is always caused by attempting to use indexing ([]) on a boolean value.

The solution involves

  • identifying where the incorrect boolean assignment is happening and ensuring that you're working with the correct data type (list, tuple, string, or dictionary) before attempting to access elements by index or key.
  • Using print(type(your_variable)).
  • Carefully tracing your code are key debugging techniques.