[Tutor] Faster list searching?

Alan Gauld alan.gauld at btinternet.com
Thu Nov 19 01:49:15 CET 2009


"Tim Peters" <tim.peters at gmail.com> wrote

> result = set(list1) - set(list2)
>
> Of course the result is a set then.  

Which means that if there were duplicates in list1 you only get 
one copy in the result. As Tim says, whether that is good, bad 
or irrelevant depends on the problem context.

> Maybe that will work fine in context, maybe not.  
> I leave it as an exercise to figure out how to
> change it back into a list (hint:  try the obvious way first ;-) ).

But that won't replace any missing duplicates.
If they are significant you'll probably need to stick with Kent's 
list comprehension approach.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list