Unique Elements in a List

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Mon May 9 20:26:16 EDT 2005


runes:

> d = {}
> for k in data:
>     try:
>         d[k] += 1
>     except:
>         d[k] = 1
>
> for k,v in d.items():
>     if v == 1:
>         print k

For this problem, if duplicated keys are common enough, this version is
probably the faster.

With this *different* problem, it seems that sometimes the faster
solution is using "in":
http://pyalot.blogspot.com/2005/04/dictionary-speed.html

Bye,
Bearophile




More information about the Python-list mailing list