[Python-ideas] Uniquify attribute for lists

Steven D'Aprano steve at pearwood.info
Fri Nov 16 19:21:24 CET 2012

On 16/11/12 23:28, Robrecht De Rouck wrote:
> Hello,
> 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)]*

That won't work for a general purpose function, because lists can hold
unhashable items, and sets require hashable.

Here's an old recipe predating sets that solves the problem in a number
of different ways. Read the comments for versions that don't lose order.


More information about the Python-ideas mailing list