title: Composite keys in dictionaries
body: <p>I am maintaining a dictionary that keeps track of the similarities between pairs of objects.<br>
  For example, this dictionary could look like this:</p>
  
  <pre><code>similarities = {
   p1: {p2: v12, p3:v13, p4:v14},
   p2: {p1: v21, p3:v23, p4:v24},
   p3: {p1: v31, p2:v32, p4:v34},
   p4: {p1: v41, p2:v42, p4:v43}
  }
  </code></pre>
  
  <p>Note, that the similarity measurement is symmetric. Therefore, <code>similarities[p1][p2]</code> is the same as <code>similarities[p2][p1]</code> i.e. <code>v12 == v21</code>.</p>
  
  <p>Sometimes, I'll need to eliminate <code>p2</code> from <code>similarities[p1]</code>; and in doing so, I'll need to remove <code>p1</code> and <code>p2</code> from all the inner dictionaries in <code>similarities</code> as well.<br>
  This is tedious and inefficient.</p>
  
  <p>So instead of maintaining a symmetric dictionary, is there a way to maintain a dictionary with a composite key so that I can lookup <code>similarities[p1,p2]</code>? </p>
  
  <p>I can't really use a <code>tuple</code> since <code>(p1, p2) != (p2, p1)</code> and I can't know a priori how to order the tuple.</p>
  
  <p>A <code>frozenset</code> is the only other container that I can think of, but that won't cut it since there may still be other keys in <code>similarities</code> that contain either <code>p1</code> or <code>p2</code> as a component. So what container could I use to solve this issue?</p>
  
  <p>Technical info:</p>
  
  <ul>
  <li>python 2.7</li>
  <li>there will always be exactly 2 elements in this "composite key"</li>
  </ul>
  
  <p>Thank you</p>