Friday, February 28, 2020

Python - List of unique dictionaries

The usual way to find just the common elements in a set is to use Python's set class. Just add all the elements to the set, then convert the set to a list, and the duplicates are gone.
The problem, of course, is that a set() can only contain hashable entries, and a dict is not hashable.
If I had this problem, my solution would be to convert each dict into a string that represents the dict, then add all the strings to a set() then read out the string values as a list() and convert back to dict.
A good representation of a dict in string form is JSON format. And Python has a built-in module for JSON (called json of course).
Or, to make it work with Python 3.x (and recent versions of numpy), you need to convert array of dicts to numpy array of strings, e.g.
import numpy as np
list(np.unique(np.array(final_list).astype(str)))

No comments:

Post a Comment