Alex Rivera | Logout

Python set to list

Asked 2011-07-26T10:35:07.467
183

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
Edit
Report

2 Answers

17

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.

answered 2013-05-24T05:26:48.973
9
s = set([1,2,3])
print [ x for x in iter(s) ]
answered 2013-01-18T04:32:07.637

Your Answer