title: Flatten a dictionary of dictionaries (2 levels deep) of lists
body: <p>I'm trying to wrap my brain around this but it's not flexible enough.</p>
  
  <p>In my Python script I have a dictionary of dictionaries of lists. (Actually it gets a little deeper but that level is not involved in this question.) I want to flatten all this into one long list, throwing away all the dictionary keys.</p>
  
  <p>Thus I want to transform</p>
  
  <pre><code>{1: {'a': [1, 2, 3], 'b': [0]},
   2: {'c': [4, 5, 1], 'd': [3, 8]}}
  </code></pre>
  
  <p>to</p>
  
  <pre><code>[1, 2, 3, 0, 4, 5, 1, 3, 8]
  </code></pre>
  
  <p>I could probably set up a map-reduce to iterate over items of the outer dictionary to build a sublist from each subdictionary and then concatenate all the sublists together.</p>
  
  <p>But that seems inefficient for large data sets, because of the intermediate data structures (sublists) that will get thrown away. Is there a way to do it in one pass?</p>
  
  <p>Barring that, I would be happy to accept a two-level implementation that works... my map-reduce is rusty!</p>
  
  <p><strong>Update:</strong>
  For those who are interested, below is the code I ended up using.</p>
  
  <p>Note that although I asked above for a list as output, what I really needed was a sorted list; i.e. the output of the flattening could be any iterable that can be sorted.</p>
  
  <pre><code>def genSessions(d):
      """Given the ipDict, return an iterator that provides all the sessions,
      one by one, converted to tuples."""
      for uaDict in d.itervalues():
          for sessions in uaDict.itervalues():
              for session in sessions:
                  yield tuple(session)
  </code></pre>
  
  <p>...</p>
  
  <pre><code># Flatten dict of dicts of lists of sessions into a list of sessions.
  # Sort that list by start time
  sessionsByStartTime = sorted(genSessions(ipDict), key=operator.itemgetter(0))
  # Then make another copy sorted by end time.
  sessionsByEndTime = sorted(sessionsByStartTime, key=operator.itemgetter(1))
  </code></pre>
  
  <p>Thanks again to all who helped.</p>
  
  <p>[Update: replaced <code>nthGetter()</code> with <code>operator.itemgetter()</code>, thanks to @intuited.]</p>