Python set to list

user825286 picture user825286 · Jul 26, 2011 · Viewed 373.7k times · Source

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

Answer

phihag picture phihag · Jul 26, 2011

Your code does work (tested with cpython 2.4, 2.5, 2.6, 2.7, 3.1 and 3.2):

>>> a = set(["Blah", "Hello"])
>>> a = list(a) # You probably wrote a = list(a()) here or list = set() above
>>> a
['Blah', 'Hello']

Check that you didn't overwrite list by accident:

>>> assert list == __builtins__.list