[Python-ideas] Uniquify attribute for lists

Andrew Barnert abarnert at yahoo.com
Sat Nov 17 00:25:31 CET 2012


Comparing your test3 and test7 to the equivalent calls with the itertools 
recipe, it's about 32% slower in 2.7 and 46% slower in 3.3. But the added 
flexibility might easily make up for the cost—it certainly does if you need it…

More interestingly, with this:

    if hashable_only is None:
        try:
            return iteruniq(iterable, key, preserve_order, True)
        except TypeError:
            return iteruniq(iterable, key, preserve_order, False)

… hashable_only=None is only 8% slower than hashable_only=False when you have 
non-hashables, and 91% faster when you don't. (And trying unique_everseen 
instead of iteruniq if hashable_only is None and preserve_order makes that 7% 
and 94%.)

The current unique_everseen is already by far the longest recipe on the 
itertools docs page, but it still might be worth updating with a synthesized 
best-of-all-options version, or actually adding to itertools instead of leaving 
as a recipe.

----- Original Message ----

> From: Jan Kaliszewski <zuo at chopin.edu.pl>
> To: python-ideas at python.org
> Sent: Fri, November 16, 2012 2:00:06 PM
> Subject: Re: [Python-ideas] Uniquify attribute for lists
> 
> Here is my quick'n'dirty implementation of different variants (with/without 
>key,  preserving/not preserving order, accepting hashable-only/any  items):
> 
> http://wklej.org/id/872623/
> 
> Timeit-ed on my  machine:
> 
> $ python3.3 iteruniq.py
> test1_nokey_noorder_hashonly  [0.08257626800332218, 0.08304202905856073,  
>0.08718552498612553]
> test2_nokey_noorder_universal [2.48601198696997,  2.4620621589710936, 
>2.453364996938035]
> test3_nokey_withorder_hashonly  [0.3661507030483335, 0.3646505419164896,  
>0.36500189593061805]
> test4_nokey_withorder_universal [7.532308181049302,  7.397191203082912, 
>7.316833758028224]
> test5_withkey_noorder_hashonly  [0.9567891559563577, 0.9690931889927015,  
>0.9598639439791441]
> test6_withkey_noorder_universal [3.142076837946661,  3.144917198107578, 
>3.150129645015113]
> test7_withkey_withorder_hashonly  [0.9493958179373294, 0.9514245060272515,  
>0.9517305289627984]
> test8_withkey_withorder_universal [10.233501984039322,  10.404869885998778,  
>10.786898656049743]
> 
> Cheers.
> *j
> 
> _______________________________________________
> Python-ideas  mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
> 



More information about the Python-ideas mailing list