I’m trying to have all element in a nested list return in the form of set through this function, but errors occurred.
list = [[0,4], [2,4], 5, [[[7,2], 3], 4]]
def setof(list):
bag = set()
for item in list:
try: bag.add(item)
except TypeError: bag.add(setof(item))
return bag
print(setof(list))
Errors:
try: bag.add(item)
TypeError: unhashable type: ‘list’
During handling of the above exception, another exception occurred:
print(setof(list))
except TypeError: bag.add(setof(item))
TypeError: unhashable the: ‘set’
Does anyone know why did this happen or how to fix it, or a better way of doing it? This is my first time here. Thanks!