delete duplicates in list

Bernard Delmée bdelmee at advalvas.REMOVEME.be
Wed Oct 29 16:17:19 EST 2003


>  >>> a = [1, 2, 2, 3]
>  >>> d = {}.fromkeys(a)
>  >>> b = d.keys()
>  >>> print b
> [1, 2, 3]

That, or using a Set (python 2.3+). Actually I seem to recall that
"The Python CookBook" still advises building a dict as the fastest
solution - if your elements can be hashed. See the details at

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

Cheers,

Bernard.





More information about the Python-list mailing list