What's the best way to delete duplicated entries in a list?

Stephen D Evans stevee at recombinant.demon.co.uk
Sat Jul 21 19:12:10 CEST 2001


Some documented reusable code for removing duplicates from a sequence:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560

Probably more than you want for your existing task.

Stephen D Evans

----

gradha at iname.com wrote:

> Hi.
>
> Given a list of strings, I am doing the following to delete duplicated
> entries:
>
> my_list.sort()
> x = 0
> while x < len (my_list) - 1:
>         if my_list[x] == my_list[x+1]:
>                 del my_list[x+1]
>         else:
>                 x = x + 1
>
> Is there some more elegant, fast, algorithm? Is this the only one
> obvious way to do it?
>
> --
>  Grzegorz Adam Hankiewicz   gradha at terra.es   http://gradha.infierno.org




More information about the Python-list mailing list