{"id":127825,"date":"2022-09-04T07:24:54","date_gmt":"2022-09-04T07:24:54","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=643827"},"modified":"2022-09-04T07:24:54","modified_gmt":"2022-09-04T07:24:54","slug":"python-typeerror-nonetype-is-not-subscriptable-fix-this-stupid-bug","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/09\/04\/python-typeerror-nonetype-is-not-subscriptable-fix-this-stupid-bug\/","title":{"rendered":"Python TypeError: NoneType is Not Subscriptable (Fix This Stupid Bug)"},"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;643827&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<p>Do you encounter the following error message?<\/p>\n<pre class=\"wp-block-preformatted\"><code>TypeError: NoneType is not subscriptable<\/code><\/pre>\n<p>You&#8217;re not alone! This short tutorial will show you why this error occurs, how to fix it, and how to never make the same mistake again. <\/p>\n<p>So, let&#8217;s get started!<\/p>\n<h2>Summary<\/h2>\n<p class=\"has-pale-cyan-blue-background-color has-background\">Python raises the <code>TypeError: NoneType is not subscriptable<\/code> if you try to index <code>x[i]<\/code> or slice <code>x[i:j]<\/code> a <code>None<\/code> value. The <code>None<\/code> type is not indexable, i.e., it 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. You can fix it by removing the indexing or slicing call, or defining the <code>__getitem__<\/code> method.<\/p>\n<h2>Example<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"762\" height=\"140\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-20.png\" alt=\" TypeError: 'NoneType' object is not subscriptable\" class=\"wp-image-16736\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-20.png 762w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-20-300x55.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-20-150x28.png 150w\" sizes=\"auto, (max-width: 762px) 100vw, 762px\" \/><\/figure>\n<\/div>\n<p>The following minimal example that leads to the error:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"3\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">x = None\nprint(x[0])\n# TypeError: 'NoneType' object is not subscriptable<\/pre>\n<p>You set the <code>variable<\/code> to the value <code>None<\/code>. The value <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">None<\/code> is not a container object, it doesn&#8217;t <em>contain <\/em>other objects. So, the code really doesn&#8217;t make any sense&#8212;which result do you expect from the indexing operation?<\/p>\n<p><em><strong>Exercise<\/strong>: Before I show you how to fix it, try to resolve the error yourself in the following interactive shell:<\/em><\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/e55b915062\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen=\"\" width=\"100%\" height=\"356\" frameborder=\"0\"><\/iframe> <\/p>\n<p>If you struggle with indexing in Python, have a look at the following articles on the Finxter blog&#8212;especially the third!<\/p>\n<p><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 Articles:<\/strong><\/p>\n<ul class=\"has-base-background-color has-background\">\n<li><a title=\"List Indexing\" rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/daily-python-puzzle-list-indexing\/\" target=\"_blank\">Indexing in Python<\/a><\/li>\n<li><a title=\"Introduction to Slicing in Python\" rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/introduction-to-slicing-in-python\/\" target=\"_blank\">Slicing in Python<\/a><\/li>\n<li><a title=\"Accessing The Index Of Iterables In Python\" rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/accessing-the-index-of-iterables-in-python\/\" target=\"_blank\">Accessing the Index of Iterables in Python<\/a> <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2b50.png\" alt=\"\u2b50\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <\/li>\n<\/ul>\n<h2>Fixes<\/h2>\n<p>You can fix the<em> non-subscriptable TypeError <\/em>by wrapping the non-indexable values into a container data type such as a <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" title=\"The Ultimate Guide to Python Lists\" target=\"_blank\" rel=\"noreferrer noopener\">list <\/a>in Python:<\/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=\"\">x = [None]\nprint(x[0])\n# None<\/pre>\n<p>The output now is the value <code>None<\/code> and the script doesn&#8217;t yield an error message anymore.<\/p>\n<p>An alternative is to define the <code>__getitem__()<\/code> method in your code:<\/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 X: def __getitem__(self, i): return f\"Value {i}\" variable = X()\nprint(variable[0])\n# Value 0\n<\/pre>\n<p class=\"has-base-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 Tutorial<\/strong>: <a href=\"https:\/\/blog.finxter.com\/python-__getitem__-magic-method\/\" data-type=\"post\" data-id=\"125374\" target=\"_blank\" rel=\"noreferrer noopener\">Python <code>__getitem__()<\/code> magic method<\/a><\/p>\n<p>You overwrite the <code>__getitem__<\/code> method that takes one (index) argument <code>i<\/code> (in addition to the obligatory <code><a rel=\"noreferrer noopener\" title=\"Python Self \u2014 An Interactive Guide with Video\" href=\"https:\/\/blog.finxter.com\/self-in-python\/\" target=\"_blank\">self<\/a><\/code> argument) and returns the <code>i<\/code>-th value of the &#8220;container&#8221;. <\/p>\n<p>In our case, we just return a string <code>\"Value 0\"<\/code> for the element <code>variable[0]<\/code> and <code>\"Value 10\"<\/code> for the element <code>variable[10]<\/code>. <\/p>\n<p class=\"has-base-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>Full Guide<\/strong>: <a href=\"https:\/\/blog.finxter.com\/python-typeerror-nonetype-object-is-not-subscriptable\/\" data-type=\"post\" data-id=\"16733\" target=\"_blank\" rel=\"noreferrer noopener\">Python Fixing This Subsctiptable Error<\/a> (General)<\/p>\n<h2>What&#8217;s Next?<\/h2>\n<p>I hope you&#8217;d be able to fix the bug in your code! Before you go, check out our <strong><em>free Python cheat sheets<\/em><\/strong> that&#8217;ll teach you the basics in Python in minimal time:<\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Do you encounter the following error message? TypeError: NoneType is not subscriptable You&#8217;re not alone! This short tutorial will show you why this error occurs, how to fix it, and how to never make the same mistake again. So, let&#8217;s get started! Summary Python raises the TypeError: NoneType is not subscriptable [&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-127825","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\/127825","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=127825"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127825\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=127825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=127825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=127825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}