Convert a list of dictionaries into a set of dictionaries
<p>How can i make a set of dictionaries from one list of dictionaries?</p>

<p>Example:</p>

<pre><code>import copy

v1 = {'k01': 'v01', 'k02': {'k03': 'v03', 'k04': {'k05': 'v05'}}}
v2 = {'k11': 'v11', 'k12': {'k13': 'v13', 'k14': {'k15': 'v15'}}}

data = []
N = 5
for i in range(N):
    data.append(copy.deepcopy(v1))
    data.append(copy.deepcopy(v2))

print data
</code></pre>

<p>How would you create a set of dictionaries from the list <code>data</code>?</p>

<p>NS: One dictionary is equal to another when they are structurally the same. That means, they got <strong>exactly</strong> the same keys and same values (recursively) </p>

<p>A cheap workaround would be to serialize your dicts, for example:</p>

<pre><code>import json

dset = set()

d1 = {'a':1, 'b':{'c':2}}
d2 = {'b':{'c':2}, 'a':1} # the same according to your definition
d3 = {'x': 42}

dset.add(json.dumps(d1, sort_keys=True))
dset.add(json.dumps(d2, sort_keys=True))
dset.add(json.dumps(d3, sort_keys=True))

for p in dset:
    print json.loads(p) 
</code></pre>

<p>In the long run it would make sense to wrap the whole thing in a class like <code>SetOfDicts</code>.</p>

python dictionary