Sick Gaming
[Tut] Python TypeError ‘set’ object is not subscriptable - 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 ‘set’ object is not subscriptable (/thread-99934.html)



[Tut] Python TypeError ‘set’ object is not subscriptable - xSicKxBot - 09-11-2022

Python TypeError ‘set’ object is not subscriptable

<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;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;}">
<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>
<figure class="wp-block-image size-full"><img loading="lazy" 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="(max-width: 724px) 100vw, 724px" /></figure>
<h2>Minimal Error Example</h2>
<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>
<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}
my_set[0]</pre>
<p>If you run this code snippet, Python raises the <code>TypeError: 'set' object is not subscriptable</code>:</p>
<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]
TypeError: 'set' object is not subscriptable</pre>
<h2>Why Does the Error Occur?</h2>
<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>
<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>
<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>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>
<h2>How to Fix the Error?</h2>
<p><strong>How to fix the <code>TypeError: 'set' object is not subscriptable</code>?</strong></p>
<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>
<p>Here’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’t occur anymore:</p>
<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:
my_list = list(my_set) # Indexing:
print(my_list[0])
# 1 # Slicing:
print(my_list[:-1])
# [1, 2]
</pre>
<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>
<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>
<p>Let’s end this article with a bit of humor, shall we? <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2>Programmer Humor</h2>
<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’t.<br /></em><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f469.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f9d4-200d-2642-fe0f.png" alt="?‍♂️" class="wp-smiley" style="height: 1em; max-height: 1em;" /><em><br />~~~</p>
<p>There are 10 types of people in the world. Those who understand trinary, those who don’t, and those who mistake it for binary.</em> <em><br /></em><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f469.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f9d4-200d-2642-fe0f.png" alt="?‍♂️" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f471-200d-2640-fe0f.png" alt="?‍♀️" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
</div>


https://www.sickgaming.net/blog/2022/09/09/python-typeerror-set-object-is-not-subscriptable/