I am trying to convert a set to a list in Python 2.6. I'm using this syntax:
first_list = [1,2,3,4]
my_set=set(first_list)
my_list = list(my_set)
However, I get the following stack trace:
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: 'set' object is not callable
How can I fix this?
[EDITED] It's seems you earlier have redefined "list", using it as a variable name, like this:
And you'l get
I'm not sure that you're creating a set with this
([1, 2])
syntax, rather a list. To create a set, you should useset([1, 2])
.These brackets are just envelopping your expression, as if you would have written:
There're not really ignored, but do nothing to your expression.
Note:
(something, something_else)
will create a tuple (but still no list).Review your first line. Your stack trace is clearly not from the code you've pasted here, so I don't know precisely what you've done.
What you wanted was
set([1, 2, 3, 4])
.The "not callable" exception means you were doing something like
set()()
- attempting to call aset
instance.Whenever you are stuck in such type of problems, try to find the datatype of the element you want to convert first by using :
Then, Use:
to convert it to a list. You can use the newly built list like any normal list in python now.
Hmmm I bet that in some previous lines you have something like:
Am I wrong ?
Instead of:
Why not shortcut the process:
This will remove the dupes from you list and return a list back to you.