Comparing list of tuples within dictionary
<p>I have been cracking my head over this problem... I have a simplified dataset like such:<br>
    <code>dict = {'A': [('a', 1, 1), ('b', 2, 2), ('c', 3, 3)], 'B': [('b', 2, 2), ('a', 1, 1)], 'C': [('d', 4, 4)], 'D': [('c', 3, 3), ('e', 5, 5)]}</code><br>
where the value represents a list of tuples.  </p>

<p>I need to find for each value in the dictionary, are the list elements found in that of another value? If yes, the keyvalue pair should be removed from the dictionary. For example, [('b', 2, 2), ('a', 1, 1)] is found in [('a', 1, 1), ('b', 2, 2), ('c', 3, 3)], so the keyvalue pair for 'B' should be removed. The final dictionary should look like:  </p>

<pre><code>dict = {'A': [('a', 1, 1), ('b', 2, 2), ('c', 3, 3)], 'C': [('d', 4, 4)], 'D': [('c', 3, 3), ('e', 5, 5)]}
</code></pre>

<p>I have searched the forums but have not found a solution to this problem... Would greatly appreciate any help with this! Thank you!  </p>

<h2>Edit:</h2>

<p>This dictionary does not contain duplicate values as I have previously removed them through mapping to another dictionary based on another set of conditions. However it may be useful to others if the solution can be extended to duplicates as well, for example keeping 1 out of all duplicates.<br>
PS: Thanks for all the responses!</p>

<p>Use <code>set</code> and <code>combinations</code>:</p>

<pre><code>from itertools import combinations

d = {'A': [('a', 1, 1), ('b', 2, 2), ('c', 3, 3)], 'B': [('b', 2, 2), ('a', 1, 1)], 'C': [('d', 4, 4)]}

xs = set()

for (ai, av), (bi, bv) in combinations(d.items(), 2):
    if set(av) &lt;= set(bv): xs.add(ai)
    if set(bv) &lt;= set(av): xs.add(bi)

for x in xs: del d[x]
print d
# {'A': [('a', 1, 1), ('b', 2, 2), ('c', 3, 3)], 'C': [('d', 4, 4)]}
</code></pre>

python dictionary