delete duplicates in list

Duncan Booth duncan at NOSPAMrcp.co.uk
Thu Oct 30 04:13:02 EST 2003


Alex Martelli <aleax at aleax.it> wrote in 
news:L7Wnb.65547$e5.2408695 at news1.tin.it:

> Your assertion:
> 
>> there should be an easier or more intuitive solution, maybe with a list
>> comprehension=
> 
> doesn't seem self-evident to me.  A list-comprehension might be, e.g:
> 
> [ x for i, x in enumerate(a) if i==a.index(x) ]
> 
> and it does have the advantages of (a) keeping order AND (b) not
> requiring hashable (nor even inequality-comparable!) elements -- BUT
> it has the non-indifferent cost of being O(N*N) while the others
> are about O(N).  If you really want something similar to your approach:
> 
>>  >>> b = [x for x in a if x not in b]
> 
> you'll have, o horrors!-), to do a loop, so name b is always bound to
> "the result list so far" (in the LC, name b is only bound at the end):
> 
> b = []
> for x in a:
>     if x not in b:
>         b.append(x)
> 
> However, this is O(N*N) too.  In terms of "easier or more intuitive",
> I suspect only this latter solution might qualify.

I dunno about more intuitive, but here's a fairly simple list comprehension 
solution which is O(N) and preserves the order. Of course its back to 
requiring hashable elements again:

>>> startList = [5,1,2,1,3,4,2,5,3,4]
>>> d = {}
>>> [ d.setdefault(x,x) for x in startList if x not in d ]
[5, 1, 2, 3, 4]

And for the 'I must do it on one line' freaks, here's the single expression 
variant of the above: :^)

>>> [ d.setdefault(x,x) for d in [{}] for x in startList if x not in d ]
[5, 1, 2, 3, 4]

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list