What this error means

You tried to use square brackets [] to access an element in a set, but sets don't support indexing. Sets are unordered collections, so they don't have positions or indices like lists do.

Common causes

  • You confused a set with a list.
  • You expected a list but got a set instead.
  • You're trying to access a specific element by position in a set.

Example that causes the error

items = {1, 2, 3}  # This is a set
first = items[0]  # TypeError: sets don't support indexing

How to fix it: convert to list

If you need indexing, convert the set to a list first:

items = {1, 2, 3}
items_list = list(items)  # Convert to list
first = items_list[0]  # OK: lists support indexing

# Note: order may be different since sets are unordered

Use set operations instead

Sets are meant for membership testing and set operations, not indexing:

items = {1, 2, 3}
if 1 in items:  # OK: check membership
    print("Found")

# Get one element (order not guaranteed):
first = next(iter(items))  # Gets an element, but which one is random

Use a list if you need order and indexing

items = [1, 2, 3]  # Use list if you need indexing
first = items[0]  # OK: lists support indexing
second = items[1]  # OK

Check the type first

data = some_function()
if isinstance(data, list):
    first = data[0]  # OK for lists
elif isinstance(data, set):
    data = list(data)  # Convert first
    first = data[0]