Best way to find unique items in a list

Aahz Maruch aahz at
Thu Feb 24 13:24:05 CET 2000

In article <20000224.10515678 at sparky.spkydomain>,
David Fisher  <aiki108 at> wrote:
>Here's a quickie.
>Def Unique(thelist):
>	uniquedict = {}
>	for i in thelist:
>		uniquedict[i] = 0
>	return uniquedict.keys()
>Whether it's any faster i don't know.

Yup.  This is, in fact, the canonical Python idiom.  Now, it's not
*guaranteed* to be faster, but it's got close to O(N) time, whereas
invoking sort() is likely to push O(NlogN) time.
                      --- Aahz (Copyright 2000 by aahz at

Androgynous poly kinky vanilla queer het    <*>
Hugs and backrubs -- I break Rule 6

Love does not conquer all.  Humans are extremely flexible, usually more
flexible than we're willing to admit.  But some flexibility comes at a
cost, and sometimes that cost is more than we can bear.

More information about the Python-list mailing list