{"id":128938,"date":"2022-10-16T19:59:58","date_gmt":"2022-10-16T19:59:58","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=796098"},"modified":"2022-10-16T19:59:58","modified_gmt":"2022-10-16T19:59:58","slug":"python-typeerror-dict_keys-not-subscriptable-fix-this-stupid-bug","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/10\/16\/python-typeerror-dict_keys-not-subscriptable-fix-this-stupid-bug\/","title":{"rendered":"Python TypeError: \u2018dict_keys\u2019 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;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;}\">\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: 'dict_keys' object 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>Solution<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">Python raises the &#8220;<code>TypeError: 'dict_keys' object is not subscriptable<\/code>&#8221; 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>\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=\"\">print(list(my_dict.keys())[0])<\/pre>\n<\/p>\n<h2>Example<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" 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\/uploads\/2022\/10\/image-135-300x46.png 300w\" sizes=\"auto, (max-width: 733px) 100vw, 733px\" \/><\/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=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">d = {1:'a', 2:'b', 3:'c'}\nprint(d.keys()[0])<\/pre>\n<p>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\\...\\code.py\", line 2, in &lt;module> print(d.keys()[0])\nTypeError: 'dict_keys' object is not subscriptable<\/pre>\n<p>Note that the same error message occurs if you use slicing instead of indexing:<\/p>\n<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'}\nprint(d.keys()[:-1]) # &lt;== same error<\/pre>\n<\/p>\n<h2>Fixes<\/h2>\n<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>\n<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>\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=\"\">print(type(d.keys()))\n# &lt;class 'dict_keys'><\/pre>\n<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&#8217;t make too much sense, would it? <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/26a1.png\" alt=\"\u26a1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<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>\n<p>Here&#8217;s an example fix:<\/p>\n<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'}\nprint(list(d.keys())[0])\n# 1<\/pre>\n<p>Here&#8217;s an other example fix:<\/p>\n<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'}\nprint(tuple(d.keys())[:-1])\n# (1, 2)<\/pre>\n<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>\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 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>\n<h2>Summary<\/h2>\n<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>\n<p>The <code>dict_keys<\/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 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>\n<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>\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<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","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Do you encounter the following error message? TypeError: &#8216;dict_keys&#8217; object 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! Solution Python raises the &#8220;TypeError: &#8216;dict_keys&#8217; object is [&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-128938","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\/128938","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=128938"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128938\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=128938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=128938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=128938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}