Removing duplicates from a list
Peter Otten
__peter__ at web.de
Wed Sep 14 08:34:06 EDT 2005
Rubinho wrote:
> I've a list with duplicate members and I need to make each entry
> unique.
>
> I've come up with two ways of doing it and I'd like some input on what
> would be considered more pythonic (or at least best practice).
>
> Method 1 (the traditional approach)
>
> for x in mylist:
> if mylist.count(x) > 1:
> mylist.remove(x)
That would be an odd tradition:
>>> mylist = [1, 2, 1, 3, 2, 3]
>>> for x in mylist:
... if mylist.count(x) > 1:
... mylist.remove(x)
...
>>> mylist
[2, 1, 2, 3] # oops!
See "Unexpected Behavior Iterating over a Mutating Object"
http://mail.python.org/pipermail/python-list/2005-September/298993.html
thread for the most recent explanation.
Rather, the traditional approach for an algorithmic problem in Python is to
ask Tim Peters, see his recipe at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560/
(which predates Python's set class).
Peter
More information about the Python-list
mailing list