[Tut] Python TypeError: NoneType is Not Subscriptable (Fix This Stupid Bug) - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] Python TypeError: NoneType is Not Subscriptable (Fix This Stupid Bug) (/thread-99910.html) |
[Tut] Python TypeError: NoneType is Not Subscriptable (Fix This Stupid Bug) - xSicKxBot - 09-06-2022 Python TypeError: NoneType is Not Subscriptable (Fix This Stupid Bug) <div> <div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{"align":"left","id":"643827","slug":"default","valign":"top","ignore":"","reference":"auto","class":"","count":"1","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}"> <div class="kksr-stars"> <div class="kksr-stars-inactive"> <div class="kksr-star" data-star="1" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="2" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="3" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="4" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="5" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> <div class="kksr-stars-active" style="width: 142.5px;"> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> </div> <div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div> </div> <p>Do you encounter the following error message?</p> <pre class="wp-block-preformatted"><code>TypeError: NoneType is not subscriptable</code></pre> <p>You’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> <p>So, let’s get started!</p> <h2>Summary</h2> <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’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> <h2>Example</h2> <div class="wp-block-image"> <figure class="aligncenter size-large"><img 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="(max-width: 762px) 100vw, 762px" /></figure> </div> <p>The following minimal example that leads to the error:</p> <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 print(x[0]) # TypeError: 'NoneType' object is not subscriptable</pre> <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’t <em>contain </em>other objects. So, the code really doesn’t make any sense—which result do you expect from the indexing operation?</p> <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> <p> <iframe loading="lazy" src="https://trinket.io/embed/python/e55b915062" marginwidth="0" marginheight="0" allowfullscreen="" width="100%" height="356" frameborder="0"></iframe> </p> <p>If you struggle with indexing in Python, have a look at the following articles on the Finxter blog—especially the third!</p> <p><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Related Articles:</strong></p> <ul class="has-base-background-color has-background"> <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> <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> <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 src="https://s.w.org/images/core/emoji/14.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" /> </li> </ul> <h2>Fixes</h2> <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> <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] print(x[0]) # None</pre> <p>The output now is the value <code>None</code> and the script doesn’t yield an error message anymore.</p> <p>An alternative is to define the <code>__getitem__()</code> method in your code:</p> <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() print(variable[0]) # Value 0 </pre> <p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" 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> <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 — 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 “container”. </p> <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> <p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" 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> <h2>What’s Next?</h2> <p>I hope you’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’ll teach you the basics in Python in minimal time:</p> </div> https://www.sickgaming.net/blog/2022/09/04/python-typeerror-nonetype-is-not-subscriptable-fix-this-stupid-bug/ |