Last updated: Apr 8, 2024
Reading timeยท3 min
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.
Here is an example of how the error occurs.
# ๐๏ธ 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.
You can pass a set
object to the list()
constructor to convert a set
to a list.
my_set = {'a', 'b', 'c'} my_list = list(my_set) print(my_list) # ๐๏ธ ['a', 'c', 'b'] print(my_list[0]) # ๐๏ธ 'a'
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.
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).
set
Alternatively, you can declare a list instead of a set
object by using square
brackets instead of curly braces.
my_list = ['a', 'b', 'c'] print(my_list[0]) # ๐๏ธ a print(my_list[1]) # ๐๏ธ b
set
.set
Tuples are declared using parentheses.
my_tuple = ('a', 'b', 'c') print(my_tuple[0]) # ๐๏ธ a print(my_tuple[1]) # ๐๏ธ b
Note that tuples are immutable. If you need a mutable data structure where the order is preserved, use a list.
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
.
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'}
If you meant to declare a dictionary, containing key-value pairs, use the following syntax.
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 will get an error. You can use a try/except statement if you need to handle that.
my_set = {'a', 'b', 'c'} my_list = list(my_set) 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.
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:
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.
a_list = [1, 2, 3] # ๐๏ธ <built-in method __getitem__ of list object at 0x7f71f3252640> print(a_list.__getitem__)
You can learn more about the related topics by checking out the following tutorials: