Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news.teledata-fn.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: Thomas Rachel Newsgroups: comp.lang.python Subject: Re: Converting a set into list Date: Sun, 15 May 2011 22:27:34 +0200 Organization: A newly installed InterNetNews server Message-ID: References: <87iptdid68.fsf@benfinney.id.au> <871v00j2bh.fsf@benfinney.id.au> <34fc571c-f382-405d-94b1-0a673da5f46b@t16g2000vbi.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit User-Agent: Mozilla/5.0 (X11; U; Linux i686; de; rv:1.9.2.14) Gecko/20110221 SUSE/3.1.8 Thunderbird/3.1.8 In-Reply-To: Lines: 29 NNTP-Posting-Date: 15 May 2011 22:30:01 CEST NNTP-Posting-Host: 8f636258.newsspool2.arcor-online.net X-Trace: DXC=1C:4K5kIJoH0YVY]kmLTlDA9EHlD;3YcB4Fo<]lROoRA8kFV6RJA X-Complaints-To: usenet-abuse@arcor.de Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5444 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 return. > The set intersection is the smartest result better than a "for" loop or a > comprehension list. I'm not sure about if it is really the smartest way. s=set(list2); [i for i in list1 if i in s] is in the same order of magnitude as the set operation. 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 set&set solution, in contrary, might require one loop while transforming to a set and another one for the & operation. > Infact the operatin loops are compiled into python, therfore they are the > fastest. Which loops do you mean here? Thomas