How to convert a set to a list in python?

PythonListSetPython 2.6

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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.

Solution 3 - Python

[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

Solution 4 - Python

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.

Solution 5 - Python

Simply type:

list(my_set)

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

Solution 6 - Python

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.

Solution 7 - Python

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).

Solution 8 - Python

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)

Solution 9 - Python

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

list = set(something)

Am I wrong ?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongathView Question on Stackoverflow
Solution 1 - PythonuserView Answer on Stackoverflow
Solution 2 - Pythontonym415View Answer on Stackoverflow
Solution 3 - PythonRuslan GrokhovetskyView Answer on Stackoverflow
Solution 4 - PythonPawan KumarView Answer on Stackoverflow
Solution 5 - PythonJesseView Answer on Stackoverflow
Solution 6 - PythonChris MorganView Answer on Stackoverflow
Solution 7 - PythonJoëlView Answer on Stackoverflow
Solution 8 - PythontmaricView Answer on Stackoverflow
Solution 9 - PythonJeannotView Answer on Stackoverflow