[Tutor] generating unique set of dicts from a list of dicts
Peter Otten
__peter__ at web.de
Tue Jan 10 22:53:15 CET 2012
[NN]
>> uniques_map = {}
>> for d in list_of_dicts:
>> uniques[dict_hash(d)] = d
>> unique_dicts = uniques_map.values()
[Dave Angel]
> 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.
Another variant:
# keys and values in the dictionaries must be hashable
list_of_dicts = ...
unique_dicts_map = {}
for d in list_of_dicts:
key = frozenset(d.items())
unique_dicts_map[key] = d
unique_dicts = unique_dicts_map.values()
I'm using a frozenset because it is hashable. It should be easy to see that
two dicts are equal if and only if they comprise a set of equal key-value
pairs.
More information about the Tutor
mailing list