Re: remove duplicates from list *preserving order*

2005-02-07 Thread Alex Martelli
Steven Bethard [EMAIL PROTECTED] wrote: ... I have a list[1] of objects from which I need to remove duplicates. I have to maintain the list order though, so solutions like set(lst), etc. will not work for me. What are my options? So far, I can see: I think the recipe by that subject in

Re: remove duplicates from list *preserving order*

2005-02-06 Thread Francis Girard
Hi, I think your last solution is not good unless your list is sorted (in which case the solution is trivial) since you certainly do have to see all the elements in the list before deciding that a given element is not a duplicate. You have to exhaust the iteratable before yielding anything.

Re: remove duplicates from list *preserving order*

2005-02-06 Thread Steven Bethard
Francis Girard wrote: I think your last solution is not good unless your list is sorted (in which case the solution is trivial) since you certainly do have to see all the elements in the list before deciding that a given element is not a duplicate. You have to exhaust the iteratable before

Re: remove duplicates from list *preserving order*

2005-02-06 Thread John Machin
Francis Girard wrote: Hi, I think your last solution is not good unless your list is sorted (in which case the solution is trivial) since you certainly do have to see all the elements in the list before deciding that a given element is not a duplicate. You have to exhaust the iteratable

Re: remove duplicates from list *preserving order*

2005-02-06 Thread Steven Bethard
John Machin wrote: So, just to remove ambiguity, WHICH one of the bunch should be retained? Short answer: the first seen is what the proverbial man in the street would expect For my purposes, it doesn't matter which instance is retained and which are removed, so yes, retaining the first one is

Re: remove duplicates from list *preserving order*

2005-02-03 Thread Carl Banks
Steven Bethard wrote: I'm sorry, I assume this has been discussed somewhere already, but I found only a few hits in Google Groups... If you know where there's a good summary, please feel free to direct me there. I have a list[1] of objects from which I need to remove duplicates. I have to

Re: remove duplicates from list *preserving order*

2005-02-03 Thread [EMAIL PROTECTED]
You could do it with a class, like this, I guess it is a bit faster than option 1, although I'm no connaisseur of python internals. -class uniquelist(list): -def __init__(self, l): -for item in l: -self.append(item) -def append(self, item): -if item not in

Re: remove duplicates from list *preserving order*

2005-02-03 Thread Larry Bates
Take a look at this recipe on ASPN: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204297 I think it might help. Larry Bates Steven Bethard wrote: I'm sorry, I assume this has been discussed somewhere already, but I found only a few hits in Google Groups... If you know where there's a

Re: remove duplicates from list *preserving order*

2005-02-03 Thread Steven Bethard
Carl Banks wrote: from itertools import * [ x for (x,s) in izip(iterable,repeat(set())) if (x not in s,s.add(x))[0] ] Wow, that's evil! Pretty cool, but for the sake of readers of my code, I think I'll have to opt against it. ;) STeVe -- http://mail.python.org/mailman/listinfo/python-list

Re: remove duplicates from list *preserving order*

2005-02-03 Thread [EMAIL PROTECTED]
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 =

Re: remove duplicates from list *preserving order*

2005-02-03 Thread Michael Spencer
Steven Bethard wrote: I'm sorry, I assume this has been discussed somewhere already, but I found only a few hits in Google Groups... If you know where there's a good summary, please feel free to direct me there. I have a list[1] of objects from which I need to remove duplicates. I have to