[Tutor] Uniques values in a list

Raymond Hettinger python@rcn.com
Sun, 17 Mar 2002 09:41:28 -0500


----- Original Message -----
From: "Alexandre Ratti" <alex@gabuzomeu.net>
To: <tutor@python.org>
Cc: "Raymond Hettinger" <python@rcn.com>
Sent: Sunday, March 17, 2002 9:46 AM
Subject: Re: [Tutor] Uniques values in a list


> Thanks for your input. I've added these solutions to my tests. They are
the
> fastest solutions by far. The downside is that they both require Python
2.2.

The first only requires Python 2.0.  I think that is when list
comprehensions and setdefault went in.

>
> >def uniq1(alist):                    # Fastest order preserving
> >     set = {}                            # memory: {key:key}*uniqcnt
> >     return [set.setdefault(e,e) for e in alist if e not in set]
> >
> >def uniq4(alist):                       # Fastest non-order preserving
> >     set = {}
> >     map(set.__setitem__, alist, [])     # memory: [None]*n +
{key:None}*u
> >     return set.keys()
>
> Here are my results when running all 5 solutions:
>
> Item count: 50000
> Using a second list...
> 0.216866328858
> Using a dictionary...
> 0.0358277278739
> Using in place filtering...
> 0.210324158427
> Filtering with list comprehension
> 0.000247238057568
> Filtering with map
> 9.47047474682e-005
>
> The last two solutions are order of magnitude faster than the others,
> that's impressive. 9.47e-005 is 0,0000947, right?

Yes!

Raymond