Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python TypeError ‘set’ object is not subscriptable

#1
Python TypeError ‘set’ object is not subscriptable

5/5 – (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: 'set' object is not subscriptable:

Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 2, in <module> my_set[0]
TypeError: 'set' object is not subscriptable

Why Does the Error Occur?


The Python TypeError: 'set' object is not subscriptable occurs if you try to access an element of a set using indexing or slicing that imply an ordering of the set.

However, sets are unordered collections of unique elements: they have no ordering of elements. Thus, you cannot use slicing or indexing, operations that are only possible on an ordered type.

? Recommended Tutorial: The Ultimate Guide to Python Sets

How to Fix the Error?


How to fix the TypeError: 'set' object is not subscriptable?

To fix the TypeError: 'set' object is not subscriptable, either convert the unordered set to an ordered list or tuple before accessing it or get rid of the indexing or slicing call altogether.

Here’s an example where you convert the unordered set to an ordered list first. Only then you use indexing or slicing so the error doesn’t occur anymore:

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]

Alternatively, you can also convert the set to a tuple to avoid the TypeError: 'set' object is not subscriptable:

my_tuple = tuple(my_set)

Let’s end this article with a bit of humor, shall we? ?

Programmer Humor


There are only 10 kinds of people in this world: those who know binary and those who don’t.
??‍♂️
~~~

There are 10 types of people in the world. Those who understand trinary, those who don’t, and those who mistake it for binary.
??‍♂️?‍♀️



https://www.sickgaming.net/blog/2022/09/...criptable/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Pandas Series Object – A Helpful Guide with Examples xSicKxBot 0 1,313 05-01-2023, 01:30 AM
Last Post: xSicKxBot
  [Tut] Python TypeError: ‘dict_keys’ Not Subscriptable (Fix This Stupid Bug) xSicKxBot 0 1,219 10-17-2022, 03:24 AM
Last Post: xSicKxBot
  [Tut] (Fixed) Python TypeError ‘bool’ object is not subscriptable xSicKxBot 0 1,440 10-02-2022, 12:23 PM
Last Post: xSicKxBot
  [Tut] Python TypeError: NoneType is Not Subscriptable (Fix This Stupid Bug) xSicKxBot 0 1,129 09-06-2022, 02:00 AM
Last Post: xSicKxBot
  [Tut] [Fixed] Matplotlib: TypeError: ‘AxesSubplot’ object is not subscriptable xSicKxBot 0 1,308 09-05-2022, 09:33 AM
Last Post: xSicKxBot
  [Tut] How to Fix TypeError: unhashable type: ‘list’ xSicKxBot 0 1,350 07-14-2022, 03:51 AM
Last Post: xSicKxBot
  [Tut] How to Get The Current Reference Count of an Object in Python? xSicKxBot 0 1,411 12-21-2020, 05:13 AM
Last Post: xSicKxBot
  [Tut] Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug) xSicKxBot 0 1,340 11-12-2020, 09:04 PM
Last Post: xSicKxBot
  [Tut] How to Solve Python “TypeError: ‘int’ object is not iterable”? xSicKxBot 0 1,320 09-18-2020, 10:49 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:

Forum software by © MyBB Theme © iAndrew 2016