python: list of lists of matching dicts
<p>I have a few dicts like:</p>

<p><code>a = [ { 'event_id': 1}, { 'event_id': 1}, { 'event_id': 1}, { 'event_id': 2}, { 'event_id': 2}, { 'event_id': 3} ]</code></p>

<p>I want a list of lists, each sublist containing all the dicts with similar <code>event_id</code> values:</p>

<p><code>[ [ { 'event_id': 1}, { 'event_id': 1}, { 'event_id': 1} ], [ { 'event_id': 2}, { 'event_id': 2} ], [ { 'event_id': 3} ] ]</code></p>

<p>Is there a quick recipe for this?</p>

<p>First, make a dictionary mapping <code>event_id</code> values to lists of dicts.</p>

<pre><code>d = {}
for dict_ in a:
    if dict_['event_id'] in d:
        d[dict_['event_id']].append(dict_)
    else:
        d[dict['event_id']] = [dict_]
</code></pre>

<p>Then make a list comprehension to gather them all into one list:</p>

<pre><code>[val for key , val in d.items()]
</code></pre>

python