title: Group emails into TO & CC with itertools.groupby and convert it to a dictionary
body: <p>I'd like to group emails by their domain and convert the result into a dictionary. So far I have figured out that <code>itertools.groupby</code> with a custom <em>func</em> will do that. It correctly assigns keys to each value, but when I try to create a dictionary only the last value is used when the values to be grouped are not continues.</p>
  <pre class="lang-py prettyprint-override"><code>
  import re
  from itertools import groupby
  
  {k: list(v) for k, v in groupby([&quot;bar&quot;, &quot;foo&quot;, &quot;baz&quot;], key=lambda x: &quot;to&quot; if re.search(r&quot;^b&quot;, x) else &quot;cc&quot;)}
  
  </code></pre>
  <p>This will produce <code>{'to': ['baz'], 'cc': ['foo']}</code> instead of <code>{'to': ['bar', 'baz'], 'cc': ['foo']}</code>.</p>
  <p>How I can fix that?</p>