Return a list of unique items of a list





7
Date Submitted Fri. Aug. 18th, 2006 9:40 AM
Revision 1 of 1
Scripter ASmith
Tags python
Comments 2 comments
Very fast method for extracting all of the unique items from a list while preserving their original ordering.

###
Author: PeterB
URL: http://www.peterbe.com/plog/uniqifiers-benchmark
###

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
 

Comments

Comments set() in Python 2.4
Tue. Sep. 12th, 2006 10:02 PM    Beginner nil_ptr
  Comments but..
Sat. Oct. 28th, 2006 8:23 PM    Newbie sk1p

Voting