title: Pythonic way to merge keys with common values for a single dictionary
body: <p>How do I merge the keys of a dictionary with common values into a tuple. For example:</p>
  
  <pre><code>A = {'E2': {'5', '7'}, 'E3': {'4', '8'}, 'E5': {'5', '7'}, 'E8': {'4', '8'}}
  
  output = {('E2', 'E5'): {'5', '7'}, ('E3', 'E8'): {'4', '8'}}
  </code></pre>
  
  <p>My attempt:</p>
  
  <pre><code>A = {'E2': {'5', '7'}, 'E3': {'4', '8'}, 'E5': {'5', '7'}, 'E8': {'4', '8'}}
  
  output = {}
  seen = []
  for k, v in A.items():
      if v not in [s[1] for s in seen]: # not seen this value yet
          print('NOT SEEN')
          print(k, v)
          seen.append([k,v])
          output[k] = v
      else: # already seen it 
          print('SEEN')
          print(k, v)
          # determine where we've seen it 
          where = [x for x in seen if x[1]==v]
          output.pop(where[0][0])
          output[(where[0][0], k)] = v
  
  
  print('OUTPUT = ', output)       
  </code></pre>
  
  <p>This prints:</p>
  
  <pre><code>OUTPUT =  {('E2', 'E5'): {'7', '5'}, ('E3', 'E8'): {'4', '8'}}
  </code></pre>