Ask Your Question

Revision history [back]

The page fastest way to uniquify a list in python addresses this; there, it's claimed that the following function is the "best" (fast, and maintains order):

def f5(seq, idfun=None): 
    # order preserving
    if idfun is None:
        def idfun(x): return x
    seen = {}
    result = []
    for item in seq:
        marker = idfun(item)
        # in old Python versions:
        # if seen.has_key(marker)
        # but in new ones:
        if marker in seen: continue
        seen[marker] = 1
        result.append(item)
    return result