PythonUtil's difference & intersection

PythonUtil.py :

def difference(a, b):
    """
    difference(list, list):
    """
    if not a: return b
    if not b: return a
    d = []
    for i in a:
        if (i not in b) and (i not in d):
            d.append(i)
    for i in b:
        if (i not in a) and (i not in d):
            d.append(i)
    return d

Why not using this :

def difference(s1,s2):
    return list((set(s1).symmetric_difference(s2)))

It’s 2383.27x faster.

test :

from direct.showbase.PythonUtil import difference
def sdifference(s1,s2):
    return list((set(s1).symmetric_difference(s2)))

a=[0,1,2,3,4,5,6,7,8,9]*1000
b=[10,1,12,3,14,5,16,7,18,9]*1000

startT=globalClock.getRealTime()
sd=sdifference(a,b)
print len(sd),sd
print globalClock.getRealTime()-startT #<----- 0.00189644796513 sec

startT=globalClock.getRealTime()
d=difference(a,b)
print len(d),d
print globalClock.getRealTime()-startT #<----- 4.51974500972 secs

Same thing for intersection.

Seems like an improvement. I think at the time this was originally written (many many years ago), Python had not yet introduced sets.

David