fix typeerror set object is not subscriptable in python

How to Fix – TypeError ‘set’ object is not subscriptable

If you are working with Python, you may encounter the error message “TypeError: ‘set’ object is not subscriptable” when trying to access an element or perform a slice operation in a set using square brackets. This error occurs because sets are not subscriptable, meaning you cannot access individual elements of a set like you can with a list or a string.

fix typeerror set object is not subscriptable in python

In this tutorial, we will explore the causes of this error and provide several solutions to fix it. We will also discuss some best practices to avoid this error in the future. So, let’s get started!

Understanding the TypeError: 'set' object is not subscriptable error

The error message here is quite helpful. It tells us that we’re trying to use a set as a subscriptible object (for example, a list, string, tuple, etc.) using the square brackets notation [] to retrieve a specific element or a slice of elements.

A set is an unordered collection of unique items, unlike sequences that are ordered. Values in a sequence have an order to them and thus they are subscriptable but there’s no inherent order to the values in a set meaning that they are not subscriptable. If you try to do so, you’ll get this error.

Let’s look at an example.

# set of scores
scores_set = {91, 77, 82, 45, 89}
# using set as a subscriptable object
print(scores_set[0])

Output:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Cell In[2], line 4
      2 scores_set = {91, 77, 82, 45, 89}
      3 # using set as a subscriptable object
----> 4 print(scores_set[0])

TypeError: 'set' object is not subscriptable

In the above example, we created a set scores_set that stores some integer values. We then tried to access the value at index 0 in the variable scores_set and we get the error TypeError: 'int' object is not subscriptable.

You’ll get the same error if you try to perform a slice operation on a set.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# set of scores
scores_set = {91, 77, 82, 45, 89}
# using set as a subscriptable object
print(scores_set[0:3])

Output:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Cell In[3], line 4
      2 scores_set = {91, 77, 82, 45, 89}
      3 # using set as a subscriptable object
----> 4 print(scores_set[0:3])

TypeError: 'set' object is not subscriptable

In the above scenarios, we are using a set as a subscriptable object which is not allowed and we get a TypeError.

Fixing the error

To fix this error, use data structures that are subscriptable such as a list or a tuple instead of a set.

Let’s revisit the examples from above and fix those errors.

# list of scores
scores_ls = [91, 77, 82, 45, 89]
# score at index 0
print(scores_ls[0])

Output:

91

Here, we are using a list instead of a set. Lists are sequences (an ordered collection of values) and thus they are subscriptable. You can see that we don’t get an error here.

You can similarly perform the slice operation.

# list of scores
scores_ls = [91, 77, 82, 45, 89]
# first three list values
print(scores_ls[0:3])

Output:

[91, 77, 82]

You can see that we do not get a TypeError here because lists are subscriptable. You can alternatively use other sequence types as well, for example, a tuple.

Conclusion

The “TypeError: ‘int’ object is not subscriptable” error occurs when we try to access an index in a set or slice a set, which is not possible as sets are unordered and hence not subscriptable. To fix this error, you can use a subscriptable type like a list or a tuple instead.

You might also be interested in –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top