remove duplicates from list *preserving order*

wittempj at hotmail.com wittempj at hotmail.com
Thu Feb 3 17:01:15 EST 2005


You could create a class based on a list which takes a list as
argument, like this:
-class uniquelist(list):
-    def __init__(self, l):
-        for item in l:
-            self.append(item)
-
-    def append(self, item):
-        if item not in self:
-            list.append(self, item)
-
-l = [1, 1, 2, 3, 3, 4]
-print l
-m = uniquelist(l)
-print m

will print
py:[1, 1, 2, 3, 3, 4]
py:[1, 2, 3, 4]




More information about the Python-list mailing list