Removing duplicates from a list

Steven Bethard steven.bethard at gmail.com
Sat Sep 17 01:55:48 EDT 2005


drochom wrote:
> i suppose this one is faster (but in most cases efficiency doesn't
> matter)
> 
>>>>def stable_unique(s):
> 
> 	e = {}
> 	ret = []
> 	for x in s:
> 		if not e.has_key(x):
> 			e[x] = 1
> 			ret.append(x)
> 	return ret

I'll repeat Peter Otten's link to Tim Peters's recipe here:
     http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560/
Read the comments at the end, they talk about order-preserving lists.

See Raymond Hettinger's response:

     def uniq(alist)    # Fastest order preserving
         set = {}
         return [set.setdefault(e,e) for e in alist if e not in set]

STeVe



More information about the Python-list mailing list