{"id":127962,"date":"2022-09-09T16:50:05","date_gmt":"2022-09-09T16:50:05","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=661363"},"modified":"2022-09-09T16:50:05","modified_gmt":"2022-09-09T16:50:05","slug":"python-typeerror-set-object-is-not-subscriptable","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/09\/09\/python-typeerror-set-object-is-not-subscriptable\/","title":{"rendered":"Python TypeError \u2018set\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;661363&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<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"724\" height=\"223\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/09\/image-5.png\" alt=\"\" class=\"wp-image-661377\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/09\/image-5.png 724w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/09\/image-5-300x92.png 300w\" sizes=\"auto, (max-width: 724px) 100vw, 724px\" \/><\/figure>\n<h2>Minimal Error Example<\/h2>\n<p>Given the following minimal example where you create a set and attempt to access an element of this set using <a href=\"https:\/\/blog.finxter.com\/daily-python-puzzle-list-indexing\/\" data-type=\"post\" data-id=\"84\" target=\"_blank\" rel=\"noreferrer noopener\">indexing<\/a> or <a href=\"https:\/\/blog.finxter.com\/introduction-to-slicing-in-python\/\" data-type=\"post\" data-id=\"731\">sli<\/a><a href=\"https:\/\/blog.finxter.com\/introduction-to-slicing-in-python\/\" data-type=\"post\" data-id=\"731\" target=\"_blank\" rel=\"noreferrer noopener\">c<\/a><a href=\"https:\/\/blog.finxter.com\/introduction-to-slicing-in-python\/\" data-type=\"post\" data-id=\"731\">ing<\/a>:<\/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=\"\">my_set = {1, 2, 3}\nmy_set[0]<\/pre>\n<p>If you run this code snippet, Python raises the <code>TypeError: 'set' object is not subscriptable<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" 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> my_set[0]\nTypeError: 'set' object is not subscriptable<\/pre>\n<h2>Why Does the Error Occur?<\/h2>\n<p>The Python <code>TypeError: 'set' object is not subscriptable<\/code> occurs if you try to access an element of a set using indexing or slicing that imply an ordering of the set. <\/p>\n<p>However, sets are <strong><em>unordered collections of unique elements<\/em><\/strong>: they have no ordering of elements. Thus, you cannot use slicing or indexing, operations that are only possible on an ordered type.<\/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>Recommended Tutorial<\/strong>: <a href=\"https:\/\/blog.finxter.com\/sets-in-python\/\" data-type=\"post\" data-id=\"1908\" target=\"_blank\" rel=\"noreferrer noopener\">The Ultimate Guide to Python Sets<\/a><\/p>\n<h2>How to Fix the Error?<\/h2>\n<p><strong>How to fix the <code>TypeError: 'set' object is not subscriptable<\/code>?<\/strong><\/p>\n<p class=\"has-global-color-8-background-color has-background\">To fix the <code>TypeError: 'set' object is not subscriptable<\/code>, either convert the unordered set to an ordered <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"post\" data-id=\"7332\" target=\"_blank\">list<\/a> or <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" data-type=\"post\" data-id=\"12043\" target=\"_blank\">tuple<\/a> before accessing it or get rid of the indexing or slicing call altogether.<\/p>\n<p>Here&#8217;s an example where you <a href=\"https:\/\/blog.finxter.com\/python-list\/\" data-type=\"post\" data-id=\"21502\" target=\"_blank\" rel=\"noreferrer noopener\">convert<\/a> the unordered set to an ordered list first. Only then you use indexing or slicing so the error doesn&#8217;t occur anymore:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">my_set = {1, 2, 3} # Convert set to list:\nmy_list = list(my_set) # Indexing:\nprint(my_list[0])\n# 1 # Slicing:\nprint(my_list[:-1])\n# [1, 2]\n<\/pre>\n<p>Alternatively, you can also <a href=\"https:\/\/blog.finxter.com\/python-tuple\/\" data-type=\"post\" data-id=\"21575\" target=\"_blank\" rel=\"noreferrer noopener\">convert<\/a> the set to a tuple to avoid the <code>TypeError: 'set' object is not subscriptable<\/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=\"\">my_tuple = tuple(my_set)<\/pre>\n<p>Let&#8217;s end this article with a bit of humor, shall we? <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f642.png\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2>Programmer Humor<\/h2>\n<p class=\"has-global-color-8-background-color has-background\"><em>There are only 10 kinds of people in this world: those who know binary and those who don\u2019t.<br \/><\/em><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f469.png\" alt=\"\ud83d\udc69\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f9d4-200d-2642-fe0f.png\" alt=\"\ud83e\uddd4\u200d\u2642\ufe0f\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><em><br \/>~~~<\/p>\n<p>There are 10 types of people in the world. Those who understand trinary, those who don\u2019t, and those who mistake it for binary.<\/em> <em><br \/><\/em><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f469.png\" alt=\"\ud83d\udc69\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f9d4-200d-2642-fe0f.png\" alt=\"\ud83e\uddd4\u200d\u2642\ufe0f\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f471-200d-2640-fe0f.png\" alt=\"\ud83d\udc71\u200d\u2640\ufe0f\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Minimal Error Example Given the following minimal example where you create a set and attempt to access an element of this set using indexing or slicing: my_set = {1, 2, 3} my_set[0] If you run this code snippet, Python raises the TypeError: &#8216;set&#8217; object is not subscriptable: Traceback (most recent call [&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-127962","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\/127962","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=127962"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127962\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=127962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=127962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=127962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}