How To Resolve TypeError: ‘Bool’ Object Is Not Subscriptable In Python

TypeError: 'bool' object is not subscriptable in Python

TypeError: ‘bool’ object is not subscriptable In Python is a common error that beginners can often encounter. In this article, I will point out common causes of errors and offer two solutions to solve this problem.

What causes the TypeError: ‘bool’ object is not subscriptable in Python?

TypeError: Python throws this error when you perform an unsupported operation on an object with an invalid data type.

The subscriptable object is a subscript object or an iterable object. 

The TypeError: ‘bool’ object is not subscriptable occurs when you perform index access or square bracket slicing a Bool object. 

Note: Objects that can be subscriptable in Python include: list, tuple, dict, and string.

Example: 

Error setting index on Bool object.

sampleBool = True

# Perform indexing on a Bool object
print(sampleBool[1])

Output:

Traceback (most recent call last):
  File "code.py", line 4, in <module>
    print(sampleBool[1])
TypeError: 'bool' object is not subscriptable

Example:

Error performing square bracket slicing on Bool object.

sampleBool = False

# Slice the boolean object
print(sampleBool[0:])

Output:

Traceback (most recent call last):
  File "code.py", line 4, in <module>
    print(sampleBool[0:])
TypeError: 'bool' object is not subscriptable

How to solve the TypeError: ‘bool’ object is not subscriptable in Python?

Convert bool objects to strings

It would be best if you converted bool objects to strings using the str() function and can perform slicing on the string using the str.slice() function.

Example:

  • Initialize a Bool object.
  • Convert the bool object to string using the str() function.
  • Do index access or do a slicing with square brackets.
sampleBool = False

# Use the str() function to convert a bool object to string
myString = str(sampleBool)

# Slice the boolean object
print('String after slicing:', myString[1:])

Output:

String after slicing: alse

Or you can do string index access.

Example:

sampleBool = False

# Use the str() function to convert a bool object to string
myString = str(sampleBool)

# Index access on the string
print('Substring in the first position:', myString[1])

Output:

Substring in the first position: a

Put the bool object in the list

Another way you can perform index access or bracket slicing of Bool objects is to put them in a list.

Example:

  • Initializes a list of elements that are Bool objects.
  • Do get the index and do the square bracket slicing on the list.
sampleList = [ True, False, True, False, True]

# Get the index from the list
print('The element at position 1 of the list:', sampleList[1]) 

# Perform slicing on the list
print('List after slicing:', sampleList[0:])

Output:

The element at position 1 of the list: False
List after slicing: [True, False, True, False, True]

Summary

Hopefully, this article will give you an idea to solve your problem. Both solutions are reasonable if you get the TypeError: ‘bool’ object is not subscriptable In Python. If you have any ideas on this topic, please leave us a comment. We are very grateful for that.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *