generating unique set of dicts from a list of dicts

Dave Angel d at davea.name
Tue Jan 10 15:49:27 EST 2012


On 01/10/2012 03:24 PM, bruce wrote:
> <SNIP>
> Since dict_hash returns a string, which is immutable, you can now use
> a dictionary to find the unique elements:
>
> uniques_map = {}
> for d in list_of_dicts:
>      uniques[dict_hash(d)] = d
> unique_dicts = uniques_map.values()
>
>>>>> *** not sure what the "uniqes" is, or what/how it should be defined....
Don't know about the rest of the message, but I think there's a typo in 
the above fragment.  On the third line, it should be uniques_map, not 
uniques that you're adding an item to.

And unless you have a really long (and strong) hash, you still have to 
check for actually equal.  In otherwords, the above solution will throw 
out a dict that happens to have the same hash as one already in the 
uniques_map.

Do you trust the  "equals" method  for your dicts ?  If not, that's your 
first problem.  If you do, then you can simply do

unique_dicts = []
for d in list_of_dicts:
      if d not in unique_dicts:
            unique_dicts.append(d)

Do it, then decide if performance is inadequate.  Only then  should you 
worry about faster methods, especially if the faster method is broken.



-- 

DaveA




More information about the Python-list mailing list