Well, the list comprehension doesn't make a lot of sense here. Even if you want to be as pythonic as possible, you don't want to run the comprehension through all the elements on A, when you can do it just in the elements of B:
def diffD(a,b): c = copy(a) for y in b: c.remove(y) return c In my laptop, this takes less than half the time than your diffA, but the time is likely to be closer when the size of B increases. Since you are sure that b is contained in a, you can use "set" instead of "Set" and it will be even faster: def diffC(a,b): return list(set(a).difference(set(b))) If you look at the source of Set.difference will see that if first check if b is a subset of a, in which case converts them to "sets" and call the corresponding method there. By using directly "set" you will save you the checking, three coercions (a from Set to set, b from Set to set, and from the output to Set again) that you don't need at all. Note, however, that the Set function will fall back to the list comprehension that you mention if B is not a subset of A. Not a problem in your case, but worth remembering. About using this for lists, there is a problem: coercing a list A into a Set (or set) will kill any duplicate elements that A might have, so one has to be careful with it. Cheers Javier On Dec 10, 9:02 am, Nathann Cohen <nathann.co...@gmail.com> wrote: > Hello !!! > > I just tried it, and I'm actually quite surprised.... > > def diffA(a,b): > return [v for v in a if v not in b] > > def diffB(a,b): > return list(Set(a).difference(Set(b))) > > n=100000 > k=10000 > a = range(n) > b = list(Set([randint(0,n) for i in range(k)])) > > time cA=diffA(a,b) > CPU times: user 29.36 s, sys: 0.09 s, total: 29.45 s > Wall time: 29.76 s > > time cB=diffB(a,b) > CPU times: user 0.03 s, sys: 0.01 s, total: 0.04 s > Wall time: 0.04 s > > sage: cB==cA > True > > With such a difference, it could be interesting even for small sets... > O_o > > It may even be interesting to directly write an function computing the > difference of two lists through Sets... > > Nathann -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org