[Python-ideas] Uniquify attribute for lists
abarnert at yahoo.com
Fri Nov 16 20:12:43 CET 2012
If I understand this right, the problem you want to solve is that there is no obvious way to uniquify lists that's order preserving and efficient, so you want a good implementation to be added as an attribute of the list type. Right?
As others have pointed out, your implementation only works for lists with hashable elements, so no lists of lists, for example.
Also, making it an attribute of list means you can't use it on, say, a tuple, or a dict key iterator, or a file. Why restrict it like that? I'd much rather have an itertools.uniquify(seq) than a list method. (If I'm just misreading your use of the word "attribute", I apologize.)
And, once it's a separate function rather than a member of list, why do you want it to return a list rather than a generator?
All that being said, if getting this right is difficult enough that a bunch of people working together on a blog over 6 years didn't come up with a good version that supports non-hashable elements, maybe a good implementation does belong in the standard library itertools.
Sent from my iPhone
On Nov 16, 2012, at 4:28, Robrecht De Rouck <de.rouck.robrecht at gmail.com> wrote:
> I just wanted to bring to your attention that an attribute for removing duplicate elements for lists would be a nice feature.
> def uniquify(lis):
> seen = set()
> seen_add = seen.add
> return [ x for x in lis if x not in seen and not seen_add(x)]
> The code is from this post. Also check out this performance comparison of uniquifying snippets.
> It would be useful to have a uniquify attribute for containers in general.
> Best regards, Robrecht
> Python-ideas mailing list
> Python-ideas at python.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas