How can I reshape and aggregate list of tuples in Python?
<p>I'm a newb to Python so apologies in advance if my question looks trivial.</p>

<p>From a psycopg2 query i have a result in the form of a list of tuples looking like:</p>

<pre><code>[(1, 0), (1, 0), (1, 1), (2, 1), (2, 2), (2, 2), (2, 2)]
</code></pre>

<p>Each tuple represents id of a location where event happened and hour of the day when event took place.</p>

<p>I'd like to reshape and aggregate this list with subtotals for each hour in each location, to a form where it looks like:</p>

<pre><code>[(1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 0), (2, 1, 1), (2, 3, 3)]
</code></pre>

<p>Where each touple will now tell me that, for example: 
in location 1, at hour 0 there were 2 events; 
in location 1, at hour 1 there was 1 event;
and so on...</p>

<p>If there were 0 events at certain hour, I still would like to see it, as for example 0 events at 0 hours in location 2: (2, 0, 0)</p>

<p>How could I implement it in Python?</p>

<p>EDIT: Thanks for help!</p>
