Converting a set into list
Thomas Rachel
nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de
Sun May 15 16:27:34 EDT 2011
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
More information about the Python-list
mailing list