title: List Comprehension to Flatten a Dictionary of Dictionaries
body: <p>I have a dictionary of dictionaries:</p>
  
  <pre><code>my_dict = {
      'a': {(1,2): True,
            (1,3): False},
      'b': {(1,4): True,
            (2,3): False}
  }
  </code></pre>
  
  <p>The dictionary is always of this form, but every 'child' dictionary has a different set of keys: <code>my_dict['a'][(1,2)]</code> exists, but that doesn't mean <code>my_dict['b'][(1,2)]</code> also exists.</p>
  
  <p>I want a list (in no particular order) of the boolean values:</p>
  
  <pre><code>[True, False, True, False]
  </code></pre>
  
  <p>I am trying to use a single list comprehension to accomplish this:</p>
  
  <pre><code>[my_dict[letter][pair] for pair in my_dict[letter] for letter in my_dict]
  </code></pre>
  
  <p>This raises an error: </p>
  
  <pre><code>---------------------------------------------------------------------------
  KeyError                                  Traceback (most recent call last)
  &lt;ipython-input-142-dc1565efcdc8&gt; in &lt;module&gt;()
        6 }
        7 
  ----&gt; 8 [my_dict[letter][pair] for pair in my_dict[letter] for letter in my_dict]
  KeyError: (2, 3)
  </code></pre>
  
  <p>It appears to be looking for (2,3) in both my_dict['a'] and my_dict['b']. I thought the comprehension I wrote would only look for the keys in the appropriate dictionary. </p>
  
  <p>I've seen <a href="https://stackoverflow.com/a/3835478/2134824">this solution</a> which can work to flatten any nested dictionary. I also know I could brute force it with imperative loops. I am just trying to understand why the list comprehension isn't working the way I have it written.</p>