Converting a set into list

Peter Otten __peter__ at web.de
Sat May 14 05:33:15 EDT 2011


TheSaint wrote:

> I've stumble to find a solution to get a list from a set
> 
> <code>
> 
>>>> aa= ['a','b','c','f']
>>>> aa
> ['a', 'b', 'c', 'f']
>>>> set(aa)

To clarify: this creates a new object, so aa is still a list.

> {'a', 'c', 'b', 'f'}
>>>> [k for k in aa]
> ['a', 'b', 'c', 'f']

So you are actually converting a list to a (new) list here. Of course it 
would have worked with a set or an arbitrary iterable, too.

> </code>
> I repute the comprehension list too expensive, is there another method?
 
mylist = list(myset)

Do you notice the similarity to converting a list to a set?




More information about the Python-list mailing list