title: Group list items in a dict
body: <p>I want to generate a dictionary from a list of dictionaries, grouping list items by the value of some key, such as:</p>
  <pre class="lang-python prettyprint-override"><code>input_list = [
          {'a':'tata', 'b': 'foo'},
          {'a':'pipo', 'b': 'titi'},
          {'a':'pipo', 'b': 'toto'},
          {'a':'tata', 'b': 'bar'}
  ]
  output_dict = {
          'pipo': [
               {'a': 'pipo', 'b': 'titi'}, 
               {'a': 'pipo', 'b': 'toto'}
           ],
           'tata': [
               {'a': 'tata', 'b': 'foo'},
               {'a': 'tata', 'b': 'bar'}
           ]
  }
  </code></pre>
  <p>So far I've found two ways of doing this. The first simply iterates over the list, create sublists in the dict for each key value and append elements matching these keys to the sublist:</p>
  <pre><code>l = [ 
      {'a':'tata', 'b': 'foo'},
      {'a':'pipo', 'b': 'titi'},
      {'a':'pipo', 'b': 'toto'},
      {'a':'tata', 'b': 'bar'}
      ]
  
  res = {}
  
  for e in l:
      res[e['a']] = res.get(e['a'], []) 
      res[e['a']].append(e)
  </code></pre>
  <p>And another using <code>itertools.groupby</code>:</p>
  <pre><code>import itertools
  from operator import itemgetter
  
  l = [ 
          {'a':'tata', 'b': 'foo'},
          {'a':'pipo', 'b': 'titi'},
          {'a':'pipo', 'b': 'toto'},
          {'a':'tata', 'b': 'bar'}
  ]
  
  l = sorted(l, key=itemgetter('a'))
  res = dict((k, list(g)) for k, g in itertools.groupby(l, key=itemgetter('a')))
  </code></pre>
  <p>I wonder which alternative is the most efficient?</p>
  <p>Is there any more pythonic/concise or better performing way of achieving this?</p>