Removing duplicates from a list

S

Steven Bethard

drochom said:
i suppose this one is faster (but in most cases efficiency doesn't
matter)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,103
Latest member
VinaykumarnNevatia
Top