10-02-2022, 12:23 PM
(Fixed) Python TypeError ‘bool’ object is not subscriptable
5/5 – (1 vote)
![[Image: image-11.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-11.png)
Problem Formulation
Consider the following minimal example where a occurs:
This yields the following output:
Solution Overview
Python raises the if you use indexing or slicing with the square bracket notation on a Boolean variable. However, the Boolean type is not indexable and you cannot slice it—it’s not iterable!
In other words, the Boolean class doesn’t define the method.
You can fix this error by
Related Tutorials: Check out our tutorials on indexing and slicing on the Finxter blog to improve your skills!
Method 1: Convert Boolean to a String
If you want to access individual characters of the “Boolean” strings and , consider converting the Boolean to a string using the built-in function. A string is subscriptable so the error will not occur when trying to index or slice the converted string.
Method 2: Put Boolean Into List
A simple way to resolve this error is to put the Boolean into a list that is subscriptable—that is you can use indexing or slicing on lists that define the magic method.
Method 3: Define the __getitem__() Magic Method
You can also define your own wrapper type around the Boolean variable that defines a dunder method for so that every indexing or slicing operation returns a specified value as defined in the dunder method.
This hack is generally not recommended, I included it just for comprehensibility and to teach you something new.![[Image: 1f609.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f609.png)
Summary
The error message “” happens if you access a boolean like a list such as or . To solve this error, avoid using slicing or indexing on a Boolean or use a subscriptable object such as lists or strings.
5/5 – (1 vote)
![[Image: image-11.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-11.png)
Problem Formulation
Consider the following minimal example where a
Code:
TypeError: 'bool' object is not subscriptableCode:
boo = True
boo[0]
# or:
boo[3:6]This yields the following output:
Code:
Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 2, in <module> boo[0]
TypeError: 'bool' object is not subscriptablePython raises the
Code:
TypeError: 'bool' object is not subscriptableIn other words, the Boolean class doesn’t define the
Code:
__getitem__()Code:
boo = True
boo[0] # Error!
boo[3:6] # Error!
boo[-1] # Error!
boo[:] # Error!You can fix this error by
- converting the Boolean to a string using the function because strings are subscriptable,Code:
str()
- removing the indexing or slicing call,
- defining a dummy method for a custom “Boolean wrapper class”.Code:
__getitem__()
Related Tutorials: Check out our tutorials on indexing and slicing on the Finxter blog to improve your skills!Method 1: Convert Boolean to a String
If you want to access individual characters of the “Boolean” strings
Code:
"True"Code:
"False"Code:
str()Code:
boo = True
boo_string = str(boo) print(boo_string[0])
# T
print(boo_string[1:-1])
# ruA simple way to resolve this error is to put the Boolean into a list that is subscriptable—that is you can use indexing or slicing on lists that define the
Code:
__getitem__()Code:
bools = [True, True, True, False, False, False, True, False]
print(bools[-1])
# False print(bools[3:-3])
# [False, False]You can also define your own wrapper type around the Boolean variable that defines a dunder method for
Code:
__getitem__()Code:
class MyBool: def __init__(self, boo): self.boo = boo def __getitem__(self, index): return self.boo my_boolean = MyBool(True) print(my_boolean[0])
# True print(my_boolean[:-1])
# TrueThis hack is generally not recommended, I included it just for comprehensibility and to teach you something new.
![[Image: 1f609.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f609.png)
Summary
The error message “
Code:
TypeError: 'boolean' object is not subscriptableCode:
booCode:
boo[0]Code:
boo[1:4]
