Converting a set into list
Ben Finney
ben+python at benfinney.id.au
Sat May 14 10:12:31 EDT 2011
TheSaint <nobody at nowhere.net.no> writes:
> Hello
>
> I've stumble to find a solution to get a list from a set
>
> <code>
>
> >>> aa= ['a','b','c','f']
Creates a new list object. Binds the name ‘aa’ to that object.
> >>> aa
> ['a', 'b', 'c', 'f']
Evaluates the object referenced by the name ‘aa’.
> >>> set(aa)
> {'a', 'c', 'b', 'f'}
Creates a new set object, populating it with the contents from the list
object referenced by ‘aa’. Doesn't do anything with the new set object,
which will soon be garbage-collected.
> >>> [k for k in aa]
> ['a', 'b', 'c', 'f']
Creates a new list object by iterating each of the items from the list
referenced by ‘aa’. Does nothing with the new list object, which will
soon be garbage-collected.
> </code>
> I repute the comprehension list too expensive, is there another method?
Another method to do what?
If you want to bind ‘aa’ to a new object, do so with an assignment
statement. (Your example has exactly one assignment statement; all the
other statements create objects which are never bound to anything.)
But what is it you actually want to do?
--
\ “The fact that I have no remedy for all the sorrows of the |
`\ world is no reason for my accepting yours. It simply supports |
_o__) the strong probability that yours is a fake.” —Henry L. Mencken |
Ben Finney
More information about the Python-list
mailing list