Best way to find unique items in a list
Alex
alex at somewhere.round.here
Thu Feb 24 16:02:25 EST 2000
How about the following:
import operator
def unique_elements(l):
d = {}
length = len(l)
map(operator.setitem, length * [d], l, length * [None])
return d.keys ()
test_list = ['Max','Gitte','Magnus','Caroline','Clara','Max','Gitte']
print unique_elements(test_list)
This returns ['Magnus', 'Gitte', 'Caroline', 'Clara', 'Max'].
You could easily sort the result if you want that.
You might also want to check out kjbuckets, which provides a set type.
Alex.
More information about the Python-list
mailing list