delete duplicates in list

Alex Martelli aleax at aleax.it
Wed Oct 29 16:21:57 EST 2003


Bernard Delmée wrote:

>>  >>> 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

Yep, but a Set is roughly as fast as a dict -- it has a small
penalty wrt a dict, but, emphasis on small.  Still, if you're
trying to squeeze every last cycle out of a bottleneck, it's
worth measuring, and perhaps moving from Set to dict.


Alex





More information about the Python-list mailing list