title: Elements of dict of sets in python
body: <p>I have a dictionary like this:</p>
  
  <pre><code>dict1 = {0: set([1, 4, 5]), 1: set([2, 6]), 2: set([3]), 3: set([0]), 4: set([1]), 5: set([2]), 6: set([])}
  </code></pre>
  
  <p>and from this dictionary I want to build another dictionary that count the occurrences of keys in dict1 in every other value ,that is the results should be:</p>
  
  <pre><code>result_dict = {0: 1, 1: 2, 2: 2, 3: 1, 4: 1, 5: 1, 6: 1}
  </code></pre>
  
  <p>My code was this :</p>
  
  <pre><code>dict1 =  {0: set([1, 4, 5]), 1: set([2, 6]), 2: set([3]), 3: set([0]), 4: set([1]), 5:set([2]), 6: set([])}
  result_dict = {}
  for pair in dict1.keys():
      temp_dict = list(dict1.keys())
  
      del temp_dict[pair]
      count = 0
      for other_pairs in temp_dict :
          if pair in dict1[other_pairs]:
              count = count + 1
      result_dict[pair] = count  
  </code></pre>
  
  <p>The problem with this code is that it is very slow with large set of data.
  Another attempt was in a single line, like this :</p>
  
  <pre><code>result_dict = dict((key ,dict1.values().count(key)) for key in dict1.keys())  
  </code></pre>
  
  <p>but it gives me wrong results, since values of dict1 are sets: </p>
  
  <pre><code>{0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0}
  </code></pre>
  
  <p>thanks a lot in advance</p>