Merge keys with the same value in dictionary of lists
<p>I have a dictionary of lists and would like to obtain just one key in case of keys with duplicated values. For example:</p>

<pre><code>dic1 = {8: [0, 4], 1: [0, 4], 7:[3], 4:[1, 5], 11:[3]}
</code></pre>

<p>resulting dictionary</p>

<pre><code>dic2 = {1: [0, 4], 7:[3], 4:[1, 5]}
</code></pre>

<p>The strategy would be to reverse the values in keys, which would become unique, and then again reverse the keys to their respective values:</p>

<pre><code>dic2 = {y: x for x, y in dic.items()}
</code></pre>

<p>But an error occurred because lists are not hashables. What I could do to obtain a dictionary with only one key in case of keys with the same value?</p>

<p>Turn the lists into tuples, which are hashable.</p>

<pre><code>dic2 = {tuple(y): x for x, y in dic.items()}
</code></pre>

<p>You can convert back into a list afterward if you like:</p>

<pre><code>result = {v:list(k) for k,v in dic2.items()}
</code></pre>

python list dictionary