Combining the Values from Multiple Keys in Dictionary Python
<p>In Python, I have the following dictionary of sets:</p>

<pre><code>{
    1: {'Hello', 'Bye'},
    2: {'Bye', 'Do', 'Action'},
    3: {'Not', 'But', 'No'},
    4: {'No', 'Yes'}
}
</code></pre>

<p>My goal is combine the keys which contain match values (like in this example, "Bye" and "No"), so the result will look like this:</p>

<pre><code>{
    1: {'Hello', 'Bye', 'Do', 'Action'},
    3: {'Not', 'But', 'No', 'Yes'}
}
</code></pre>

<p>Is there a way to do this?</p>

<p>If there is no overlapping matches:</p>

<pre><code>a = {1: {'Hello', 'Bye'}, 2: {'Bye', 'Do', 'Action'}, 3: {'Not', 'But', 'No'}, 4: {'No', 'Yes'}}
output = {}
for k, v in a.items():
    if output:
        for k_o, v_o in output.items():
            if v_o.intersection(v):
                output[k_o].update(v)
                break
        else:
            output[k] = v
    else:
        output[k] = v
print(output)
</code></pre>

<p>Output:</p>

<pre><code>{1: {'Action', 'Bye', 'Do', 'Hello'}, 3: {'But', 'No', 'Not', 'Yes'}}
</code></pre>

python dictionary