Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python TypeError: ‘dict_keys’ Not Subscriptable (Fix This Stupid Bug)

#1
Python TypeError: ‘dict_keys’ Not Subscriptable (Fix This Stupid Bug)

<div>
<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;796098&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;}">
<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: 'dict_keys' object 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>Solution</h2>
<p class="has-global-color-8-background-color has-background">Python raises the “<code>TypeError: 'dict_keys' object is not subscriptable</code>” if you use indexing or slicing on the <code>dict_keys</code> object obtained with <code>dict.keys()</code>. To solve the error, convert the <code>dict_keys</code> object to a list such as in <code>list(my_dict.keys())[0]</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="">print(list(my_dict.keys())[0])</pre>
</p>
<h2>Example</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="733" height="112" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-135.png" alt="" class="wp-image-796122" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-135.png 733w, https://blog.finxter.com/wp-content/uplo...300x46.png 300w" sizes="(max-width: 733px) 100vw, 733px" /></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="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">d = {1:'a', 2:'b', 3:'c'}
print(d.keys()[0])</pre>
<p>Output:</p>
<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\...\code.py", line 2, in &lt;module> print(d.keys()[0])
TypeError: 'dict_keys' object is not subscriptable</pre>
<p>Note that the same error message occurs if you use slicing instead of indexing:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">d = {1:'a', 2:'b', 3:'c'}
print(d.keys()[:-1]) # &lt;== same error</pre>
</p>
<h2>Fixes</h2>
<p>The reason this error occurs is that the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-dict-keys-method/" data-type="post" data-id="37711" target="_blank">dictionary.keys()</a></code> method returns a <code>dict_keys</code> object that is not subscriptable. </p>
<p>You can use the <code><a href="https://blog.finxter.com/python-type/" data-type="post" data-id="23967" target="_blank" rel="noreferrer noopener">type()</a></code> function to check it for yourself:</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="">print(type(d.keys()))
# &lt;class 'dict_keys'></pre>
<p class="has-global-color-8-background-color has-background"><strong>Note</strong>: You cannot expect <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-get-first-key-value-in-a-dictionary/" data-type="post" data-id="514603" target="_blank">dictionary keys to be ordered</a>, so using indexing on a non-ordered type wouldn’t make too much sense, would it? <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/26a1.png" alt="⚡" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>You can fix the<em> non-subscriptable TypeError </em>by converting the non-indexable <code>dict_keys</code> object to an indexable container type such as a <a title="The Ultimate Guide to Python Lists" rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank">list </a>in Python using the <code><a href="https://blog.finxter.com/python-list/" data-type="post" data-id="21502" target="_blank" rel="noreferrer noopener">list()</a></code> or <code>tuple()</code> function.</p>
<p>Here’s an example fix:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">d = {1:'a', 2:'b', 3:'c'}
print(list(d.keys())[0])
# 1</pre>
<p>Here’s an other example fix:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">d = {1:'a', 2:'b', 3:'c'}
print(tuple(d.keys())[:-1])
# (1, 2)</pre>
<p>Both lists and tuples are subscriptable so you can use indexing and slicing after converting the <code>dict_keys</code> object to a list or a tuple.</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 rel="noreferrer noopener" href="https://blog.finxter.com/python-typeerror-nonetype-object-is-not-subscriptable/" data-type="post" data-id="16733" target="_blank">Python Fixing This Subsctiptable Error</a> (General)</p>
<h2>Summary</h2>
<p>Python raises the <code>TypeError: 'dict_keys' object is not subscriptable</code> if you try to index <code>x[i]</code> or slice <code>x[i:j]</code> a <code>dict_keys</code> object.</p>
<p>The <code>dict_keys</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 converting the dictionary keys to a list using the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-list/" data-type="post" data-id="21502" target="_blank">list()</a></code> built-in function.</p>
<p>Alternatively, you can also fix this by removing the indexing or slicing call, or defining the <code>__getitem__</code> method. Although the previous approach is often better.</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>
<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>
</div>


https://www.sickgaming.net/blog/2022/10/...tupid-bug/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016