A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2005-September/339492.html below:

Removing duplicates from a list

Removing duplicates from a listmartijn 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4