Search code examples
pythonpython-3.xsettuples

How do You Convert a List of Strings into a Frozenset and Add it to a Set of Frozensets?


I have a variable with n words, separated by whitespaces. str.split() returns a list of these words, which is then converted to a tuple with tuple(), which is finally converted to a frozenset and added to the set of frozensets. For some reason this returns:

TypeError: 'builtin_function_or_method' object is not subscriptable

my code:

set = set([])
str = 'a b c d'
set.add[frozenset(tuple(set.split()))]

problems already addressed :

  1. tried to make a set of sets`
  2. tried to make a frozenset out of lists
  3. accidentally used .append instead of .add

(Btw this is my first question on stack overflow)


Solution

  • first, you should not name your vars with reserved names/built-ins. then, set.add is a function, use parens not brackets. and finally you don't need the tuple step.

    my_set = set()
    my_set.add(frozenset('a b c d'.split()))