Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] (Fixed) Python TypeError ‘bool’ object is not subscriptable

#1
(Fixed) Python TypeError ‘bool’ object is not subscriptable

5/5 – (1 vote)

Problem Formulation


Consider the following minimal example where a TypeError: 'bool' object is not subscriptable occurs:

boo = True
boo[0]
# or:
boo[3:6]

This yields the following output:

Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 2, in <module> boo[0]
TypeError: 'bool' object is not subscriptable

Solution Overview


Python raises the TypeError: 'bool' object is not subscriptable 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 __getitem__() method.

boo = True
boo[0] # Error!
boo[3:6] # Error!
boo[-1] # Error!
boo[:] # Error!

You can fix this error by

  1. converting the Boolean to a string using the str() function because strings are subscriptable,
  2. removing the indexing or slicing call,
  3. defining a dummy __getitem__() method for a custom “Boolean wrapper class”.

? 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 "True" and "False", consider converting the Boolean to a string using the str() built-in function. A string is subscriptable so the error will not occur when trying to index or slice the converted string.

boo = True
boo_string = str(boo) print(boo_string[0])
# T
print(boo_string[1:-1])
# ru

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 __getitem__() magic method.

bools = [True, True, True, False, False, False, True, False]
print(bools[-1])
# False print(bools[3:-3])
# [False, False]

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 __getitem__() so that every indexing or slicing operation returns a specified value as defined in the dunder method.

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])
# True

This hack is generally not recommended, I included it just for comprehensibility and to teach you something new. ?

Summary


The error message “TypeError: 'boolean' object is not subscriptable” happens if you access a boolean boo like a list such as boo[0] or boo[1:4]. To solve this error, avoid using slicing or indexing on a Boolean or use a subscriptable object such as lists or strings.



https://www.sickgaming.net/blog/2022/10/...criptable/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] [Fixed] Access Denied – OpenAI Error Reference Number 1020 xSicKxBot 0 1,419 08-10-2023, 09:24 PM
Last Post: xSicKxBot
  [Tut] [Fixed] Access Denied – OpenAI Error Reference Number 1020 xSicKxBot 0 1,389 05-25-2023, 04:45 PM
Last Post: xSicKxBot
  [Tut] Pandas Series Object – A Helpful Guide with Examples xSicKxBot 0 1,313 05-01-2023, 01:30 AM
Last Post: xSicKxBot
  [Tut] [Fixed] Sendy Loading Lists Not Working (Loads Forever) xSicKxBot 0 1,204 02-14-2023, 08:12 PM
Last Post: xSicKxBot
  [Tut] (Fixed) ModuleNotFoundError: No Module Named ‘git’ | Python xSicKxBot 0 1,213 11-22-2022, 07:46 PM
Last Post: xSicKxBot
  [Tut] ModuleNotFoundError: No Module Named ‘ffmpeg’ (Fixed) xSicKxBot 0 1,267 11-19-2022, 12:20 PM
Last Post: xSicKxBot
  [Tut] [Fixed] Python ModuleNotFoundError: No Module Named ‘readline’ xSicKxBot 0 1,280 11-11-2022, 10:01 PM
Last Post: xSicKxBot
  [Tut] Python TypeError: ‘dict_keys’ Not Subscriptable (Fix This Stupid Bug) xSicKxBot 0 1,220 10-17-2022, 03:24 AM
Last Post: xSicKxBot
  [Tut] How to Convert Bool (True/False) to a String in Python? xSicKxBot 0 1,308 10-04-2022, 11:37 AM
Last Post: xSicKxBot
  [Tut] 3 Best Ways to Generate a Random Number with a Fixed Amount of Digits in Python xSicKxBot 0 1,222 09-27-2022, 10:20 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016