[Tutor] List to dictionary

Kent Johnson kent37 at tds.net
Thu Dec 7 12:05:56 CET 2006


Luke Paireepinart wrote:
> How about this :D
> 
> # Remove duplicates from a list:
>>>> L = [1,2,2,3,3,3]
>>>> [x for x in L if x not in locals()['_[1]'].__self__]
> [1,2,3]
> 
> [accessed at 
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204297 ]

The problems with this are, it is not portable, even across versions of 
CPython (try the above in Python 2.4) and it will have poor performance 
for lists with many unique entries because it searches the entire list 
for each addition.

These recipes are interesting, particularly the discussion for the first 
one:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/438599

Raymond Hettinger (who should know, he is a Python performance guru) 
said in 2002 that this is the fastest order-preserving method:
def uniq(alist)    # Fastest order preserving
     set = {}
     return [set.setdefault(e,e) for e in alist if e not in set]

Perhaps there is now a faster method using a real set, but maybe not 
because set doesn't have a method that adds to the set and returns the 
value.

Kent



More information about the Tutor mailing list