how to remove duplicated elements in a list?
Tim N. van der Leeuw
tim.leeuwvander at nl.unisys.com
Tue Dec 20 08:28:35 EST 2005
Another way to do this, that also maintains order, is:
>>> l = [3, 1, 'a', '@', -4, 'z', 'r', 1, '@', -4]
>>> s = set()
>>> l2 = []
>>> for v in l:
... if not v in s:
... s.add(v)
... l2.append(v)
...
>>> l2
[3, 1, 'a', '@', -4, 'z', 'r']
I have no idea whether or not this is more efficient than the other
method; try it on big lists if you want to know. (But I like the idea
that I'm not looking up indexes in the old list all the time, which
just feels slow to me)
cheers,
--Tim
More information about the Python-list
mailing list