Re: Converting a set into list

2011-05-16 Thread Peter Otten
Daniel Kluev wrote: Both solutions seem to be equivalent in that concerns the number of needed loop runs, but this two-step operation might require one less loop over list1. The setset solution, in contrary, might require one loop while transforming to a set and another one for the

Re: Converting a set into list

2011-05-16 Thread Duncan Booth
Chris Torek nos...@torek.net wrote: x = [3, 1, 4, 1, 5, 9, 2, 6] x [3, 1, 4, 1, 5, 9, 2, 6] list(set(x)) [1, 2, 3, 4, 5, 6, 9] Of course, this trick only works if all the list elements are hashable. This might not be the best example since the result is

Re: Converting a set into list

2011-05-16 Thread TheSaint
Thomas Rachel wrote: Which loops do you mean here? list(set) has been proved to largely win against list = [] for item in set: list.append(item) or [list.append(item) for item in set] -- goto /dev/null -- http://mail.python.org/mailman/listinfo/python-list

Re: Converting a set into list

2011-05-16 Thread Ben Finney
TheSaint nob...@nowhere.net.no writes: Thomas Rachel wrote: Which loops do you mean here? list(set) has been proved to largely win against list = [] for item in set: list.append(item) or [list.append(item) for item in set] Remember that the criterion of speed is a matter of the

Re: Converting a set into list

2011-05-16 Thread Chris Torek
Chris Torek nos...@torek.net wrote: x = [3, 1, 4, 1, 5, 9, 2, 6] list(set(x)) This might not be the best example since the result is sorted by accident, while other list(set(...)) results are not. In article Xns9EE772D313153duncanbooth@127.0.0.1, Duncan Booth

Re: Converting a set into list

2011-05-15 Thread SigmundV
I think the OP wants to find the intersection of two lists. list(set(list1) set(list2)) is indeed one way to achieve this. [i for i in list1 if i in list2] is another one. Sigmund On May 15, 4:11 am, Chris Torek nos...@torek.net wrote: In article 871v00j2bh@benfinney.id.au Ben Finney  

Re: Converting a set into list

2011-05-15 Thread SigmundV
I'm sorry I top posted. I'll remember not to top post next time. Sigmund -- http://mail.python.org/mailman/listinfo/python-list

Re: Converting a set into list

2011-05-15 Thread TheSaint
SigmundV wrote: I think the OP wants to find the intersection of two lists. list(set(list1) set(list2)) is indeed one way to achieve this. [i for i in list1 if i in list2] is another one Exactly. I was confused on that I wasn't able to have a list in return. The set intersection is the

Re: Converting a set into list

2011-05-15 Thread TheSaint
Chris Torek wrote: x = ['three', 'one', 'four', 'one', 'five'] x ['three', 'one', 'four', 'one', 'five'] list(set(x)) ['four', 'five', 'three', 'one'] Why one *one* has purged out? Removing double occurences in a list? -- goto /dev/null --

Re: Converting a set into list

2011-05-15 Thread Steven D'Aprano
On Mon, 16 May 2011 00:05:44 +0800, TheSaint wrote: Chris Torek wrote: x = ['three', 'one', 'four', 'one', 'five'] x ['three', 'one', 'four', 'one', 'five'] list(set(x)) ['four', 'five', 'three', 'one'] Why one *one* has purged out? Removing double occurences in a list? Break the

Re: Converting a set into list

2011-05-15 Thread TheSaint
Steven D'Aprano wrote: s = set() s.add(42) s.add(42) s.add(42) print s set([42]) Good to know. I'll remember it -- goto /dev/null -- http://mail.python.org/mailman/listinfo/python-list

Re: Converting a set into list

2011-05-15 Thread Roy Smith
In article 34fc571c-f382-405d-94b1-0a673da5f...@t16g2000vbi.googlegroups.com, SigmundV sigmu...@gmail.com wrote: I think the OP wants to find the intersection of two lists. list(set(list1) set(list2)) is indeed one way to achieve this. [i for i in list1 if i in list2] is another one. Both

Re: Converting a set into list

2011-05-15 Thread Thomas Rachel
Am 15.05.2011 17:56 schrieb TheSaint: SigmundV wrote: I think the OP wants to find the intersection of two lists. list(set(list1) set(list2)) is indeed one way to achieve this. [i for i in list1 if i in list2] is another one Exactly. I was confused on that I wasn't able to have a list in

Re: Converting a set into list

2011-05-15 Thread Daniel Kluev
Both solutions seem to be equivalent in that concerns the number of needed loop runs, but this two-step operation might require one less loop over list1. The setset solution, in contrary, might require one loop while transforming to a set and another one for the operation. python -m timeit

Converting a set into list

2011-05-14 Thread TheSaint
Hello 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) {'a', 'c', 'b', 'f'} [k for k in aa] ['a', 'b', 'c', 'f'] /code I repute the comprehension list too expensive, is there another method? -- goto /dev/null --

Re: Converting a set into list

2011-05-14 Thread Peter Otten
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

Re: Converting a set into list

2011-05-14 Thread Ben Finney
TheSaint nob...@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',

Re: Converting a set into list

2011-05-14 Thread TheSaint
Peter Otten wrote: mylist = list(myset) Do you notice the similarity to converting a list to a set? There was something confusing me yesterday in doing that, but (for me strangely) I got cleared out. The point was that after a result from: newset= set(myset1) set(myset2) list= [newset]

Re: Converting a set into list

2011-05-14 Thread TheSaint
Ben Finney wrote: Another method to do what? Sorry, some time we expect to have said it as we thought it. The example was to show that after having made a set set(aa) the need to get that set converted into a list. My knowledge drove me to use a comprehension list as a converter. In another

Re: Converting a set into list

2011-05-14 Thread Chris Angelico
On Sun, May 15, 2011 at 12:14 AM, TheSaint nob...@nowhere.net.no wrote: newset= set(myset1) set(myset2) list= [newset] [{'bla', 'alb', 'lab'}] Probably list(set) is not like [set]. list(set) creates a list out of the set. [set] creates a list with one element, the set itself. It's not a

Re: Converting a set into list

2011-05-14 Thread Ben Finney
TheSaint nob...@nowhere.net.no writes: The example was to show that after having made a set set(aa) the need to get that set converted into a list. As pointed out: you already know how to create a set from an object; creating a list from an object is very similar: list(set(aa)) But

Re: Converting a set into list

2011-05-14 Thread Chris Torek
In article 871v00j2bh@benfinney.id.au Ben Finney ben+pyt...@benfinney.id.au wrote: As pointed out: you already know how to create a set from an object; creating a list from an object is very similar: list(set(aa)) But why are you doing that? What are you trying to achieve? I have no