title: Can list comprehensions (ideally) do the equivalent of 'count(*)...group by...' in SQL?
body: <p>I think list comprehensions may give me this, but I'm not sure: any elegant solutions in Python (2.6) in general for selecting unique objects in a list and providing a count?</p>
  
  <p>(I've defined an <code>__eq__</code> to define uniqueness on my object definition).</p>
  
  <p>So in RDBMS-land, something like this:</p>
  
  <pre><code>CREATE TABLE x(n NUMBER(1));
  INSERT INTO x VALUES(1);
  INSERT INTO x VALUES(1);
  INSERT INTO x VALUES(1);
  INSERT INTO x VALUES(2);
  
  SELECT COUNT(*), n FROM x
  GROUP BY n;
  </code></pre>
  
  <p>Which gives:</p>
  
  <pre><code>COUNT(*) n
  ==========
  3        1
  1        2
  </code></pre>
  
  <p>So , here's my equivalent list in Python:</p>
  
  <pre><code>[1,1,1,2]
  </code></pre>
  
  <p>And I want the same output as the SQL SELECT gives above.</p>
  
  <p>EDIT: The example I gave here was simplified, I'm actually processing lists of user-defined object-instances: just for completeness I include the extra code I needed to get the whole thing to work:</p>
  
  <pre><code>import hashlib
  
  def __hash__(self):
      md5=hashlib.md5()
      [md5.update(i) for i in self.my_list_of_stuff]
      return int(md5.hexdigest(),16)
  </code></pre>
  
  <p>The <code>__hash__</code> method was needed to get the <code>set</code> conversion to work (I opted for the list-comprehension idea that works in 2.6 [despite the fact that I learnt that involves an inefficiency (see comments) - my data set is small enough for that not be an issue]). <code>my_list_of_stuff</code> above is a list of (Strings) on my object definition.</p>