Best way to find unique items in a list

maxm maxm at normik.dk
Thu Feb 24 05:31:32 EST 2000


I have written the following snippet to return a list of unique items in
another list. I just wondered if there was a smarter/faster way of doing it?

Anyone for a little brain gym?

------------------------------------------

def Unique(theList):
    uniqueList = []
    OldValue = ''
    theList.sort()
    for value in theList:
        if OldValue != value:
            uniqueList.append(value)
        OldValue = value
    return uniqueList


print Unique(['Max','Gitte','Magnus','Caroline','Clara','Max','Gitte'])

>>>['Caroline', 'Clara', 'Gitte', 'Magnus', 'Max']

------------------------------------------

Regards

Max M Rasmussen,   New Media Director    http://www.normik.dk   Denmark
e-mail  mailto:maxm at normik.dk    private mailto:maxmcorp at worldonline.dk






More information about the Python-list mailing list