delete duplicates in list

Alex Martelli aleax at aleax.it
Wed Oct 29 16:17:31 EST 2003


christof hoeke wrote:
   ...
> i have a list from which i want a simpler list without the duplicates

Canonical is:

import sets
simplerlist = list(sets.Set(thelist))

if you're allright with destroying order, as your example solution suggests.
But dict.fromkeys(a).keys() is probably faster.  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.


Alex





More information about the Python-list mailing list