Removing duplicates from a list

martijn at gamecreators.nl martijn at gamecreators.nl
Thu Sep 15 10:53:40 EDT 2005


Ow thanks , i'm I newbie and I did this test. (don't know if this is
the best way to do a small speed test)

import timeit

def unique2(keys):
    unique = []
    for i in keys:
        if i not in unique:unique.append(i)
    return unique

def unique3(s):
    e = {}
    ret = []
    for x in s:
        if not e.has_key(x):
            e[x] = 1
            ret.append(x)
    return ret

tmp = [0,1,2,4,2,2,3,4,1,3,2]
s = """\
    try:
        str.__nonzero__
    except AttributeError:
        pass
    """
t = timeit.Timer(stmt=s)
print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
print tmp
print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
print unique2(tmp)
print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
print unique3(tmp)
print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
---------------------
5.80 usec/pass
[0, 1, 2, 4, 2, 2, 3, 4, 1, 3, 2]
7.51 usec/pass
[0, 1, 2, 4, 3]
6.93 usec/pass
[0, 1, 2, 4, 3]
6.45 usec/pass <--- your code unique2(s):




More information about the Python-list mailing list