TypeError: 'set' object is not subscriptable in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: 'set' object is not subscriptable in Python #

The Python "TypeError: 'set' object is not subscriptable in Python" occurs when we try to access a set object at a specific index, e.g. my_set[0]. To solve the error, use square brackets to declare a list, because set objects are unordered and not subscriptable.

typeerror set object is not subscriptable

Here is an example of how the error occurs.

main.py
# 👉️ if you meant to use list, do: my_list = ['a', 'b', 'c'] my_set = {'a', 'b', 'c'} # ⛔️ TypeError: 'set' object is not subscriptable print(my_set[0])

Notice that we used curly braces, so we declared a set object.

Set objects are an unordered collection of unique elements, so they don't support indexing and slicing.

You can use the in operator to check if a set contains a specific value or you can iterate over a set.

main.py
my_set = {'a', 'b', 'c'} print('a' in my_set) # 👉️ True for el in my_set: print(el) my_set.add('d') my_set.remove('b') print(my_set) # 👉️ {'d', 'a', 'c'}

Alternatively, you can declare a list instead of a set object by using square brackets instead of curly braces.

main.py
my_list = ['a', 'b', 'c'] print(my_list[0]) # 👉️ a print(my_list[1]) # 👉️ b
Notice that we use square brackets to declare a list and curly braces with comma-separated elements to declare a set.

You can pass a set object to the list() constructor to convert a set to a list, but since set objects are unordered, running the same code multiple times would produce a list containing the same elements in different order.

main.py
my_set = {'a', 'b', 'c'} my_list = list(my_set) print(my_list) # 👉️ ['a', 'c', 'b'] print(my_list[0]) # 👉️ 'a'

If you meant to declare a dictionary, containing key-value pairs, use the following syntax.

main.py
my_dict = {'name': 'Alice', 'age': 30} print(my_dict['name']) # 👉️ "Alice" print(my_dict['age']) # 👉️ 30

Notice that we separate the keys and values with a colon when declaring a dictionary.

Note that if you try to access a list index that is out of bounds, you would get an error. You can use a try/except statement if you need to handle that.

main.py
my_list = ['a', 'b', 'c'] try: print(my_list[100]) except IndexError: print('index out of bounds') # 👉️ this runs

The example catches the IndexError that is thrown if the index is out of bounds.

The "TypeError: object is not subscriptable" means that we are using square brackets to either access a key in a specific object or to access a specific index, however the object doesn't support this functionality.

Set objects are unordered and are therefore not subscriptable. To solve the error, we have to use a list or a dictionary instead of a set or remove the square brackets that try to access an index or a key in the set.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.