title: Splitting a dictionary by key suffixes
body: <p>I have a dictionary like so</p>
  
  <pre><code>d = {"key_a":1, "anotherkey_a":2, "key_b":3, "anotherkey_b":4}
  </code></pre>
  
  <p>So the values and key names are not important here. The key (no pun intended) thing, is that related keys share the same <em>suffix</em> in my example above that is <code>_a</code> and <code>_b</code>.</p>
  
  <p>These suffixes are <strong>not</strong> known before hand (they are not always <code>_a</code> and <code>_b</code> for example, and there are an unknown number of different suffixes.</p>
  
  <p>What I would like to do, is to extract out related keys into their own dictionaries, and have all generated dictionaries in a list.
  The output from above would be</p>
  
  <p><code>output = [{"key_a":1, "anotherkey_a":2},{"key_b":3, "anotherkey_b":4}]</code></p>
  
  <p>My current approach is to first get all the suffixes, and then generate the sub-dicts one at a time and append to the new list</p>
  
  <pre><code>output = list()
  # Generate a set of suffixes
  suffixes = set([k.split("_")[-1] for k in d.keys()])
  
  # Create the subdict and append to output
  for suffix in suffixes:
      output.append({k:v for k,v in d.items() if k.endswith(suffix)})
  </code></pre>
  
  <p>This <strong>works</strong> (and is not prohibitively slow or anyhting) but I am simply wondering if there is a more elegant way to do it with a list or dict comprehension? Just out of interest...</p>