163

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?

nabster
  • 1,561
  • 2
  • 20
  • 32
gath
  • 24,504
  • 36
  • 94
  • 124
  • 2
    Works for me on Python 2.6.6 on Linux... but that first line doesn't create a set. – detly Jul 06 '11 at 09:10
  • 2
    According `TypeError: 'set' object is not callable`: What is `set`? Thanks – eat Jul 06 '11 at 09:13
  • Are you sure the Traceback is from that piece of code? Works fine for me on Python 2.6.6 (and on http://ideone.com/6dYZj). But as already mentioned, `my_set` is already a `list`, not a set. – Shawn Chin Jul 06 '11 at 09:18
  • Are you sure you posted your actual code ? `my_set` is not a set not even a tuple; it's a list. However, the stack trace is weird, are you sure that in the scope `list`refers to the builtin list type? – Jeannot Jul 06 '11 at 09:23
  • Edited the question to add the correct code. – gath Jul 06 '11 at 09:24
  • 15
    You've shadowed the set builtin. Perhaps you accidently typed `set=set(first_list)` or something. Now `set` is redefined to this set object which would cause that error. Try again with a fresh Python interpreter – John La Rooy Jul 06 '11 at 09:35
  • 3
    @gnibbler: you should put that as an answer, as that's clearly what it was that did break it. – Chris Morgan Jul 06 '11 at 10:02
  • 2
    It's too bad the question has been edited to correct the problem. It's confusing to see correct code followed by an error it will not generate. In fact, the original question before the edits doesn't cause the error shown, either. I suspect this is a copy and paste error. – Mr. Lance E Sloan Sep 02 '16 at 15:49
  • `del set; set([1,2,3,4]);`, you're using "set" as a variable – Harshal Gajjar Apr 09 '18 at 18:36

9 Answers9

225

It is already a list:

>>> type(my_set)
<class 'list'>

Do you want something like:

>>> my_set = set([1, 2, 3, 4])
>>> my_list = list(my_set)
>>> print(my_list)
[1, 2, 3, 4]

EDIT: Output of your last comment:

>>> my_list = [1,2,3,4]
>>> my_set = set(my_list)
>>> my_new_list = list(my_set)
>>> print(my_new_list)
[1, 2, 3, 4]

I'm wondering if you did something like this:

>>> set = set()
>>> set([1, 2])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object is not callable
Paul P
  • 3,346
  • 2
  • 12
  • 26
user
  • 17,781
  • 20
  • 98
  • 124
  • 4
    When i do type(my_set) i get – gath Jul 06 '11 at 09:19
  • 2
    @gath, that was before you edited the code. Your current code works fine on my machine. Can you try copy pasting that into a python interpreter as it is and paste the output here – user Jul 06 '11 at 09:28
  • 1
    try this: >>> my_list = [1,2,3,4] >>> my_set = set(my_list) >>> my_new_list = list(my_set) Traceback (most recent call last): File "", line 1, in TypeError: 'set' object is not callable – gath Jul 06 '11 at 09:33
  • I exited the interpreter then back in and now its working fine. Am a bit puzzled though and am yet to figure out what 'set' was!! – gath Jul 06 '11 at 09:40
  • 1
    I bet he accidentally bound the name 'set' to {1,2,3} or [1,2,3] or something like that. (Because I did accidentally while experimenting) – user3556757 Nov 03 '14 at 10:55
  • 1
    Converting an integer list using this method is not reliable: if your set is `([1,2,12,6])` and you do `list(([1,2,12,6]))` you will get a list like `[1,2,1,2,6]` – PradyJord Dec 26 '14 at 06:06
14

Instead of:

first_list = [1,2,3,4]
my_set=set(first_list)
my_list = list(my_set)

Why not shortcut the process:

my_list = list(set([1,2,3,4])

This will remove the dupes from you list and return a list back to you.

tonym415
  • 151
  • 1
  • 5
  • 9
    There is no difference between both. You just inlined the variable assignements. –  Sep 28 '12 at 10:26
10

[EDITED] It's seems you earlier have redefined "list", using it as a variable name, like this:

list = set([1,2,3,4]) # oops
#...
first_list = [1,2,3,4]
my_set=set(first_list)
my_list = list(my_set)

And you'l get

Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: 'set' object is not callable
5

Whenever you are stuck in such type of problems, try to find the datatype of the element you want to convert first by using :

type(my_set)

Then, Use:

  list(my_set) 

to convert it to a list. You can use the newly built list like any normal list in python now.

Pawan Kumar
  • 2,132
  • 2
  • 16
  • 10
5

Simply type:

list(my_set)

This will turn a set in the form {'1','2'} into a list in the form ['1','2'].

Jesse
  • 63
  • 1
  • 7
2

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.

>>> my_set=([1,2,3,4])
>>> my_set
[1, 2, 3, 4]
>>> type(my_set)
<type 'list'>
>>> list(my_set)
[1, 2, 3, 4]
>>> type(_)
<type 'list'>

What you wanted was set([1, 2, 3, 4]).

>>> my_set = set([1, 2, 3, 4])
>>> my_set
set([1, 2, 3, 4])
>>> type(my_set)
<type 'set'>
>>> list(my_set)
[1, 2, 3, 4]
>>> type(_)
<type 'list'>

The "not callable" exception means you were doing something like set()() - attempting to call a set instance.

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
0

I'm not sure that you're creating a set with this ([1, 2]) syntax, rather a list. To create a set, you should use set([1, 2]).

These brackets are just envelopping your expression, as if you would have written:

if (condition1
    and condition2 == 3):
    print something

There're not really ignored, but do nothing to your expression.

Note: (something, something_else) will create a tuple (but still no list).

Joël
  • 2,723
  • 18
  • 36
  • @gath your edit seems fine to me, at least the first block. Are you still having the error? Can you try this in a new command-line? What do you get when typing just `set`? – Joël Jul 06 '11 at 12:31
0

Python is a dynamically typed language, which means that you cannot define the type of the variable as you do in C or C++:

type variable = value

or

type variable(value)

In Python, you use coercing if you change types, or the init functions (constructors) of the types to declare a variable of a type:

my_set = set([1,2,3])
type my_set

will give you <type 'set'> for an answer.

If you have a list, do this:

my_list = [1,2,3]
my_set = set(my_list)
tmaric
  • 5,347
  • 4
  • 42
  • 75
-1

Hmmm I bet that in some previous lines you have something like:

list = set(something)

Am I wrong ?

Jeannot
  • 1,165
  • 7
  • 18
  • As mention above in one answers, you also have the same assumption. In the main code of question was probably set `list` to be equal to creating set `set(values)`. And it was making confusion. Your answer should not be downvoted. But also it is not hooking te readers :) We are just skimming and seeing wrong thing downvote. – Vladimir Vukanac Nov 04 '19 at 10:13