Skip to main content

How to Solve "TypeError: 'set' object is not subscriptable" in Python

The TypeError: 'set' object is not subscriptable error in Python occurs when you try to access elements of a set using indexing (square brackets []), which is not supported. Sets are unordered collections of unique elements, and therefore do not have a defined order or indices.

This guide explains why this error occurs and provides the correct ways to work with set data.

Understanding Subscriptability and Sets

In Python, "subscriptable" means that you can access elements of an object using square brackets with an index or key, like this: my_object[index_or_key]. Lists, tuples, strings, and dictionaries are subscriptable:

  • Lists: my_list[2] (accesses the element at index 2)
  • Tuples: my_tuple[0] (accesses the element at index 0)
  • Strings: my_string[5] (accesses the character at index 5)
  • Dictionaries: my_dict['key'] (accesses the value associated with the key 'key')

Sets, however, are not subscriptable. Sets are:

  • Unordered: Elements have no defined order.
  • Unique: Sets can not contain duplicate elements.

Because sets are unordered, the concept of an "index" doesn't apply. There's no "first" or "third" element in a set in the way there is in a list or tuple.

my_set = {'a', 'b', 'c'}

# ⛔️ TypeError: 'set' object is not subscriptable
# print(my_set[0]) # This causes the error

Solutions

If you're getting the TypeError: 'set' object is not subscriptable error, you likely need to use a different data structure or a different approach. Here are the common solutions:

Converting a Set to a List (If Order Matters)

If you need to access elements by index and the order of elements is important, convert the set to a list:

my_set = {'a', 'b', 'c'}
my_list = list(my_set) # Convert the set to a list

print(my_list) # Output: ['a', 'c', 'b'] (order *might* be different)
print(my_list[0]) # Output: 'a' (or whatever is at index 0 now)
warning

The order of elements in a set is arbitrary and not guaranteed. When you convert a set to a list, the order of elements in the list might be different from the order in which you added them to the set. If order is important, you shouldn't have been using a set in the first place.

Converting a Set to a Tuple (If Order Matters and Immutability is Desired)

If you need to access elements by index, and the order of elements is important, and you do not need to modify the elements use a tuple()

my_set = {'a', 'b', 'c'}
my_tuple = tuple(my_set) # Convert set to tuple
print(my_tuple[0]) # Output: a
  • Tuples are ordered and indexed collections, unlike sets.

Using a List Instead of a Set (If Order and/or Duplicates are Needed)

If you find yourself needing to index into a collection, a set is likely the wrong data structure. Consider using a list instead, if order matters and/or you need to allow duplicate elements:

my_list = ['a', 'b', 'c']  # Use a list from the start
print(my_list[0]) # Output: a (This is perfectly valid)

Using a Dictionary Instead of a Set

If you need to assign values to the items, use a dictionary:

my_dict = {'name': 'Alice', 'age': 30}

print(my_dict['name']) # Output: "Alice"
print(my_dict['age']) # Output: 30
  • The dictionaries will preserve the order of the key-value pairs.
  • You can access the values assigned to keys using square bracket notation.

Using in for Membership Testing (Correct Set Usage)

If you simply want to check if a value is present in a set, use the in operator. This is what sets are designed for, and it's very efficient:

my_set = {'a', 'b', 'c'}

print('a' in my_set) # Output: True
print('d' in my_set) # Output: False

This is the correct way to use a set. You don't need indexing for this.

Iterating Over a Set

You can iterate over the elements of a set using a for loop. You just can't access them by index within the loop:

my_set = {'a', 'b', 'c'}

for element in my_set:
print(element) # Output: a, b, c (in some arbitrary order)

my_set.add('d')
my_set.remove('b')
print(my_set) # Output: {'c', 'a', 'd'}

Conclusion

The TypeError: 'set' object is not subscriptable error occurs because sets are unordered collections and don't support indexing.

  • If you need indexing, use a list or a tuple.
  • If you genuinely need a set (for uniqueness and fast membership testing), use the in operator to check for the presence of an element, or iterate over the set using a for loop without attempting to use indices.
  • If you must have both uniqueness and indexing, consider using an ordered set implementation (available in some third-party libraries) or a dictionary where the keys are the unique elements and the values are their indices (though this is often a sign that you should rethink your data structure).