Unique dictionary on basis of multiple keys
<p>I have a dict with different "types" -> modified and deleted.
I want do unique this.</p>

<pre><code>myDict = 
[
{'type': 'deleted', 'target': {'id': u'1', 'foo': {'value': ''}}},
{'type': 'modified', 'target': {'id': u'1', 'foo': {'value': ''}}},
{'type': 'deleted', 'target': {'id': u'1', 'foo': {'value': ''}}},

{'type': 'deleted', 'target': {'id': u'2', 'foo': {'value': ''}}},
{'type': 'deleted', 'target': {'id': u'2', 'foo': {'value': ''}}},
{'type': 'deleted', 'target': {'id': u'2', 'foo': {'value': ''}}},

{'type': 'deleted', 'target': {'id': u'3', 'foo': {'value': ''}}},
{'type': 'modified', 'target': {'id': u'3', 'foo': {'value': ''}}},
{'type': 'deleted', 'target': {'id': u'3', 'foo': {'value': ''}}}
]
</code></pre>

<p>To achieve a unique list I do this:</p>

<pre><code>dict((v['target']['id'],v) for v in myDict).values()

[
{'type': 'deleted', 'target': {'foo': {'value': ''}, 'id': u'1'}}, 
{'type': 'deleted', 'target': {'foo': {'value': ''}, 'id': u'2'}},
{'type': 'deleted', 'target': {'foo': {'value': ''}, 'id': u'3'}} 
]
</code></pre>

<p>How can I get a unique list on the basis of a "couple of keys".</p>

<p>I need both 'types'. My expected result is:</p>

<pre><code>[
{'type': 'deleted', 'target': {'foo': {'value': ''}, 'id': u'1'}}, 
{'type': 'modified', 'target': {'foo': {'value': ''}, 'id': u'1'}},

{'type': 'deleted', 'target': {'foo': {'value': ''}, 'id': u'2'}},
{'type': 'modified', 'target': {'foo': {'value': ''}, 'id': u'2'}},

{'type': 'deleted', 'target': {'foo': {'value': ''}, 'id': u'3'}}
]
</code></pre>

<p>Not sure I've understood your question, but, is this what you want?</p>

<pre><code>from collections import defaultdict
import json

my_list = [
    {'type': 'deleted', 'target': {'id': u'1', 'foo': {'value': ''}}},
    {'type': 'modified', 'target': {'id': u'1', 'foo': {'value': ''}}},
    {'type': 'deleted', 'target': {'id': u'1', 'foo': {'value': ''}}},

    {'type': 'deleted', 'target': {'id': u'2', 'foo': {'value': ''}}},
    {'type': 'deleted', 'target': {'id': u'2', 'foo': {'value': ''}}},
    {'type': 'deleted', 'target': {'id': u'2', 'foo': {'value': ''}}},

    {'type': 'deleted', 'target': {'id': u'3', 'foo': {'value': ''}}},
    {'type': 'modified', 'target': {'id': u'3', 'foo': {'value': ''}}},
    {'type': 'deleted', 'target': {'id': u'3', 'foo': {'value': ''}}}
]

out = defaultdict(set)

for v in my_list:
    out[v["type"]].add(json.dumps(v["target"], sort_keys=True))

result = []
for k, v in out.iteritems():
    for vv in out[k]:
        result.append({
            "type": k,
            "target": json.loads(vv)
        })

print out
print len(out["deleted"])
print len(out["modified"])
</code></pre>

python dictionary