{"id":128530,"date":"2022-10-01T19:12:31","date_gmt":"2022-10-01T19:12:31","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=742932"},"modified":"2022-10-01T19:12:31","modified_gmt":"2022-10-01T19:12:31","slug":"fixed-python-typeerror-bool-object-is-not-subscriptable","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/10\/01\/fixed-python-typeerror-bool-object-is-not-subscriptable\/","title":{"rendered":"(Fixed) Python TypeError \u2018bool\u2019 object is not subscriptable"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;742932&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"761\" height=\"225\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-11.png\" alt=\"\" class=\"wp-image-742949\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-11.png 761w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-11-300x89.png 300w\" sizes=\"auto, (max-width: 761px) 100vw, 761px\" \/><\/figure>\n<\/div>\n<h2>Problem Formulation<\/h2>\n<p>Consider the following minimal example where a <code>TypeError: 'bool' object is not subscriptable<\/code> occurs:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">boo = True\nboo[0]\n# or:\nboo[3:6]<\/pre>\n<p>This yields the following output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Traceback (most recent call last): File \"C:\\Users\\xcent\\Desktop\\code.py\", line 2, in &lt;module> boo[0]\nTypeError: 'bool' object is not subscriptable<\/pre>\n<h2>Solution Overview<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">Python raises the <code>TypeError: 'bool' object is not subscriptable<\/code> 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&#8212;it&#8217;s not <a href=\"https:\/\/blog.finxter.com\/iterators-iterables-and-itertools\/\" data-type=\"post\" data-id=\"29507\" target=\"_blank\" rel=\"noreferrer noopener\">iterable<\/a>! <\/p>\n<p>In other words, the Boolean class doesn&#8217;t define the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-__getitem__-magic-method\/\" data-type=\"post\" data-id=\"125374\" target=\"_blank\">__getitem__()<\/a><\/code> method. <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">boo = True\nboo[0] # Error!\nboo[3:6] # Error!\nboo[-1] # Error!\nboo[:] # Error!<\/pre>\n<p>You can fix this error by<\/p>\n<ol>\n<li>converting the Boolean to a string using the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-str-function\/\" data-type=\"post\" data-id=\"23735\" target=\"_blank\">str()<\/a><\/code> function because strings are subscriptable,<\/li>\n<li>removing the indexing or slicing call,<\/li>\n<li>defining a dummy <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-__getitem__-magic-method\/\" data-type=\"post\" data-id=\"125374\" target=\"_blank\">__getitem__()<\/a><\/code> method for a custom &#8220;Boolean wrapper class&#8221;.<\/li>\n<\/ol>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Related Tutorials<\/strong>: Check out our tutorials on <a href=\"https:\/\/blog.finxter.com\/daily-python-puzzle-list-indexing\/\" data-type=\"post\" data-id=\"84\" target=\"_blank\" rel=\"noreferrer noopener\">indexing<\/a> and <a href=\"https:\/\/blog.finxter.com\/introduction-to-slicing-in-python\/\" data-type=\"post\" data-id=\"731\" target=\"_blank\" rel=\"noreferrer noopener\">slicing<\/a> on the Finxter blog to improve your skills!<\/p>\n<h2>Method 1: Convert Boolean to a String<\/h2>\n<p>If you want to access individual characters of the &#8220;Boolean&#8221; strings <code>\"True\"<\/code> and <code>\"False\"<\/code>, consider converting the Boolean to a string using the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-str-function\/\" data-type=\"post\" data-id=\"23735\" target=\"_blank\">str()<\/a><\/code> built-in function. A string is subscriptable so the error will not occur when trying to index or slice the converted string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">boo = True\nboo_string = str(boo) print(boo_string[0])\n# T\nprint(boo_string[1:-1])\n# ru\n<\/pre>\n<h2>Method 2: Put Boolean Into List<\/h2>\n<p>A simple way to resolve this error is to put the Boolean into a list that is subscriptable&#8212;that is you can use indexing or slicing on lists that define the <code>__getitem__()<\/code> magic method. <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"1\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">bools = [True, True, True, False, False, False, True, False]\nprint(bools[-1])\n# False print(bools[3:-3])\n# [False, False]<\/pre>\n<h2>Method 3: Define the __getitem__() Magic Method<\/h2>\n<p>You can also define your own wrapper type around the Boolean variable that defines a dunder method for <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-__getitem__-magic-method\/\" data-type=\"post\" data-id=\"125374\" target=\"_blank\">__getitem__()<\/a><\/code> so that every indexing or slicing operation returns a specified value as defined in the dunder method. <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class MyBool: def __init__(self, boo): self.boo = boo def __getitem__(self, index): return self.boo my_boolean = MyBool(True) print(my_boolean[0])\n# True print(my_boolean[:-1])\n# True\n<\/pre>\n<p>This hack is generally not recommended, I included it just for comprehensibility and to teach you something new. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f609.png\" alt=\"\ud83d\ude09\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2>Summary<\/h2>\n<p class=\"has-global-color-8-background-color has-background\"><strong>The error message &#8220;<code>TypeError: 'boolean' object is not subscriptable<\/code>&#8221; happens if you access a boolean <code>boo<\/code> like a list such as <code>boo[0]<\/code> or <code>boo[1:4]<\/code>. To solve this error, avoid using slicing or indexing on a Boolean or use a subscriptable object such as lists or strings.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Problem Formulation Consider the following minimal example where a TypeError: &#8216;bool&#8217; object is not subscriptable occurs: boo = True boo[0] # or: boo[3:6] This yields the following output: Traceback (most recent call last): File &#8220;C:\\Users\\xcent\\Desktop\\code.py&#8221;, line 2, in &lt;module> boo[0] TypeError: &#8216;bool&#8217; object is not subscriptable Solution Overview Python raises the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-128530","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=128530"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128530\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=128530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=128530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=128530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}