get rid of duplicate elements in list without set
castironpi at gmail.com
Sat Mar 21 01:12:51 CET 2009
On Mar 20, 5:07 pm, Michael Spencer <m... at telcopartners.com> wrote:
> Alexzive wrote:
> And, if you really want, you can get the body of this into 1-line, noting that
> seen.add returns None, so the expression (item in seen or seen.add(item))
> evaluates to True if item is in seen, or None (and item is added to seen) if not.
> >>> seen = set()
> >>> B= [item for item in A if not (item in seen or seen.add(item))]
> >>> B
> [1, 2, 3, 4]
IYO in your opinion, is '... or seen.add(item) is None' more or less
You might even want '... or ( lambda x: False )( seen.add( item ) )'.
Or: '... or seen.add(item) and False'.
This preserves order.
More information about the Python-list