182

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

7 Answers7

271

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
phihag
  • 278,196
  • 72
  • 453
  • 469
  • 2
    I just copied and pasted this exact code in IDLE; I get the error. –  Jul 26 '11 at 10:43
  • Can you provide the output of `dir(set)` and `print set`? – Susam Pal Jul 26 '11 at 10:46
  • `['and', 'class', 'cmp', 'contains', 'delattr', 'doc', 'eq', 'format', 'ge', 'getattribute', 'gt', 'hash', 'iand', 'init', 'ior', 'isub', 'iter', 'ixor', 'le', 'len', 'lt', 'ne', 'new', 'or', 'rand', 'reduce', 'reduce_ex', 'repr', 'ror', 'rsub', 'rxor', 'setattr', 'sizeof', 'str', 'sub', 'subclasshook', 'xor', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']` (removed __ because of char limit) –  Jul 26 '11 at 10:48
  • @Judge John Deed At which line do you get the error? Your `set` looks fine. – phihag Jul 26 '11 at 10:51
  • try Set instead of set : REF: https://docs.python.org/2/library/sets.html – Gonzalo Mar 13 '18 at 18:57
76

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

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
29

before you write set(XXXXX) you have used "set" as a variable e.g.

set = 90 #you have used "set" as an object
…
…
a = set(["Blah", "Hello"])
a = list(a)
The Hungry Dictator
  • 3,444
  • 5
  • 37
  • 53
Zhang Yiwei
  • 303
  • 3
  • 5
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.

Nick
  • 354
  • 2
  • 6
9
s = set([1,2,3])
print [ x for x in iter(s) ]
peerxu
  • 623
  • 1
  • 6
  • 13
4

Your code works with Python 3.2.1 on Win7 x64

a = set(["Blah", "Hello"])
a = list(a)
type(a)
<class 'list'>
Pierre GM
  • 19,809
  • 3
  • 56
  • 67
Wendal Chen
  • 258
  • 3
  • 11
2

Try using combination of map and lambda functions:

aList = map( lambda x: x, set ([1, 2, 6, 9, 0]) )

It is very convenient approach if you have a set of numbers in string and you want to convert it to list of integers:

aList = map( lambda x: int(x), set (['1', '2', '3', '7', '12']) )
IgobyE
  • 21
  • 3