How can I convert a set to a list in Python? Using
a = set(["Blah", "Hello"])
a = list(a)
doesn't work. It gives me:
TypeError: 'set' object is not callable
You've shadowed the builtin set by accidentally using it as a variable name, here is a simple way to replicate your error
>>> set=set()
>>> set=set()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object is not callable
The first line rebinds set to an instance of set. The second line is trying to call the instance which of course fails.
Here is a less confusing version using different names for each variable. Using a fresh interpreter
>>> a=set()
>>> b=a()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object is not callable
Hopefully it is obvious that calling a is an error
This will work:
>>> t = [1,1,2,2,3,3,4,5]
>>> print list(set(t))
[1,2,3,4,5]
However, if you have used "list" or "set" as a variable name you will get the:
TypeError: 'set' object is not callable
eg:
>>> set = [1,1,2,2,3,3,4,5]
>>> print list(set(set))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
Same error will occur if you have used "list" as a variable name.
setchange it because you are shadowing the builtin functionset. - mouadTypeErroris the name of the type, not the variable name - phihaglist = some_setsomewhere. Addprint listbefore you call it. - Jochen Ritzel