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>You can use <a href="https://docs.python.org/2/library/sets.html" rel="nofollow">sets</a> to check whether one value is contained (<code>set.issubset(otherset)</code>) within one another.
The below code demonstrates how you could implement this very easily. For each value every other value is checked for inclusion. This can definitly be optimized.</p>

<pre><code># input data
d = {'A': [('a', 1, 1), ('b', 2, 2), ('c', 3, 3)], 'B': [('b', 2, 2), ('a', 1, 1)], 'C': [('d', 4, 4)]}

# convert each value to a set
ds = { k: frozenset(v) for k,v in d.items() }
# filter out contained values
keys = [ k1 for k1,v1 in ds.items() if [k2 for k2,v2 in ds.items() if v1.issubset(v2)] == [k1] ]
# map filtered keys to original dict
df = dict(zip(keys, map(d.get, keys)))
# df = {'A': [('a', 1, 1), ('b', 2, 2), ('c', 3, 3)], 'C': [('d', 4, 4)]}
</code></pre>

<h3>Explanation</h3>

<p>When calculating <code>keys</code> for each pair <code>(k1,v1)</code> the following is done.
All other pairs <code>(k2,v2)</code> (including <code>(k1,v1)</code>) are checked for <code>v1.issubset(v2)</code>. For all pairs satisfying this condition, the key (<code>k2</code>) is recorded and stored in a list. If no <em>other</em> value does include our given value <code>v1</code> this list will only contain <code>k1</code>, as the <code>issubset</code>relation is reflexive (<code>v.issubset(v)</code> is always <code>True</code>). Thus, when the returned list is exactly <code>[k1]</code>, we are fine.
If the list contains more values than <code>k1</code>, <code>v1</code> is contained in some other value and have to be filtered.</p>

<h3>Duplicate values</h3>

<p>The problem is not defined for duplicate values in the map. My solution keeps track of the original keys, thus for duplicate values both pairs are dropped. You can also introduce custom behavior here, like only keeping the pair with the 'smallest' key for all duplicates.</p>

<p>Other solutions keep all pairs, thus the returned mapping still contains duplicate values.</p>

<p>This modified version of <code>keys</code> can handle the case mentioned above:</p>

<pre><code>keys = [ k1 for k1,v1 in ds.items()
            if not any(v1 &lt; v2 for v2 in ds.values())
               and k1 == min(k2 for k2,v2 in ds.items() if v1 == v2) ]
</code></pre>

python dictionary