Entry
Is there a built in Python function that eliminates duplicates in a list?
Jun 27th, 2000 06:16
unknown unknown, Bjorn Pettersen, Aahz Maruch, jerry_spicklemire
def unique(lst):
d = {}
for item in lst:
d[item] = None
return d.keys()
--------
The Timbot recommends
d[item] = 1
because there's no lookup involved on 1, so it's fractionally faster.
(Any other constant should probably work the same.)
--------
Just a minor "heads up" for a possible "gotcha".
It looks to me as if this method, though speedy, will return the
cleaned list in a much different order than the original, since the
dict.keys()
method outputs in the (seemingly random) order of the dicts internal
hash table.
The moral is, if the order of the list members matters, be sure to wait
until after the cleaning step to sort, or at least remember to re-sort!