TypeError: 'set' object is not subscriptable in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# 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.

# Convert the set to a list to access it at an index

You can pass a set object to the list() constructor to convert a set to a list.

main.py
my_set = {'a', 'b', 'c'} my_list = list(my_set) print(my_list) # ๐Ÿ‘‰๏ธ ['a', 'c', 'b'] print(my_list[0]) # ๐Ÿ‘‰๏ธ 'a'

convert the set to list to access it at an index

However, since set objects are unordered, running the same code multiple times would produce a list containing the same elements in a different order.

You can also convert the set to a tuple to access an element at an index.

main.py
my_set = {'a', 'b', 'c'} my_list = tuple(my_set) print(my_list) # ๐Ÿ‘‰๏ธ ('a', 'b', 'c') print(my_list[0]) # ๐Ÿ‘‰๏ธ 'a'

Tuples are very similar to lists, but implement fewer built-in methods and are immutable (cannot be changed).

# Use square brackets to declare a list instead of a set

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

use square brackets to declare list instead of set

Notice that we use square brackets to declare a list and curly braces with comma-separated elements to declare a set.

# Use parentheses to declare a tuple instead of a set

Tuples are declared using parentheses.

main.py
my_tuple = ('a', 'b', 'c') print(my_tuple[0]) # ๐Ÿ‘‰๏ธ a print(my_tuple[1]) # ๐Ÿ‘‰๏ธ b

use parentheses to declare tuple instead of set

Note that tuples are immutable. If you need a mutable data structure where the order is preserved, use a list.

# Use the in operator to check if a value is in a set

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'}

use in operator to check if value is in set

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.

# Accessing a list at an index that doesn't exist causes an error

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

main.py
my_set = {'a', 'b', 'c'} my_list = list(my_set) try: print(my_list[100]) except IndexError: print('index out of bounds') # ๐Ÿ‘‰๏ธ this runs

accessing list at index that does not exist causes an error

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.

You should only use square brackets to access subscriptable objects.

The subscriptable objects in Python are:

  • list
  • tuple
  • dictionary
  • string

All other objects have to be converted to a subscriptable object by using the list(), tuple(), dict() or str() classes to be able to use bracket notation.

Subscriptable objects implement the __getitem__ method whereas non-subscriptable objects do not.

main.py
a_list = [1, 2, 3] # ๐Ÿ‘‡๏ธ <built-in method __getitem__ of list object at 0x7f71f3252640> print(a_list.__getitem__)

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev