09-11-2022, 08:12 AM
Python TypeError ‘set’ object is not subscriptable
5/5 – (1 vote)
![[Image: image-5.png]](https://blog.finxter.com/wp-content/uploads/2022/09/image-5.png)
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:
If you run this code snippet, Python raises the:
Why Does the Error Occur?
The Python 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?
To fix the, 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:
Alternatively, you can also convert the set to a tuple to avoid the:
Let’s end this article with a bit of humor, shall we?![[Image: 1f642.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png)
Programmer Humor
There are only 10 kinds of people in this world: those who know binary and those who don’t.
![[Image: 1f469.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f469.png)
![[Image: 1f9d4-200d-2642-fe0f.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f9d4-200d-2642-fe0f.png)
~~~
There are 10 types of people in the world. Those who understand trinary, those who don’t, and those who mistake it for binary.
![[Image: 1f469.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f469.png)
![[Image: 1f9d4-200d-2642-fe0f.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f9d4-200d-2642-fe0f.png)
5/5 – (1 vote)
![[Image: image-5.png]](https://blog.finxter.com/wp-content/uploads/2022/09/image-5.png)
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:
Code:
my_set = {1, 2, 3}
my_set[0]If you run this code snippet, Python raises the
Code:
TypeError: 'set' object is not subscriptableCode:
Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 2, in <module> my_set[0]
TypeError: 'set' object is not subscriptableThe Python
Code:
TypeError: 'set' object is not subscriptableHowever, 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 SetsHow to Fix the Error?
How to fix the
Code:
TypeError: 'set' object is not subscriptableTo fix the
Code:
TypeError: 'set' object is not subscriptableHere’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:
Code:
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
Code:
TypeError: 'set' object is not subscriptableCode:
my_tuple = tuple(my_set)Let’s end this article with a bit of humor, shall we?
![[Image: 1f642.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png)
Programmer Humor
There are only 10 kinds of people in this world: those who know binary and those who don’t.
![[Image: 1f469.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f469.png)
![[Image: 1f9d4-200d-2642-fe0f.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f9d4-200d-2642-fe0f.png)
~~~
There are 10 types of people in the world. Those who understand trinary, those who don’t, and those who mistake it for binary.
![[Image: 1f469.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f469.png)
![[Image: 1f9d4-200d-2642-fe0f.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f9d4-200d-2642-fe0f.png)

