delete duplicates in list

christof hoeke csad7 at yahoo.com
Thu Oct 30 16:10:31 EST 2003


Alex Martelli wrote:

> 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
> 
i was looking at the cookbook site but could not find the solution in 
the short time i was looking, so thanks for the link to Bernard.

but thanks to all for the interesting discussion which i at least partly 
able to follow as i am still a novice pythoner.

as for speed i did not care really as my script will not be used regularly.
but it seems i guessed right using pythons dictionary functions, it 
still seems kind of the easiest and fastest ways (i should add that the 
  list i am using consists only of strings, which can be dictionary keys 
then).


thanks again
chris




thanks to all





More information about the Python-list mailing list