title: Append to a dict of lists with a dict comprehension
body: <p>Suppose I have a large list of words. For an example:</p>
  
  <pre><code>&gt;&gt;&gt; with open('/usr/share/dict/words') as f:
  ...     words=[word for word in f.read().split('\n') if word]
  </code></pre>
  
  <p>If I wanted to build an index by first letter of this word list, this is easy:</p>
  
  <pre><code>d={}
  for word in words:
     if word[0].lower() in 'aeiou':
         d.setdefault(word[0].lower(),[]).append(word)
         # You could use defaultdict here too...
  </code></pre>
  
  <p>Results in something like this:</p>
  
  <pre><code>{'a':[list of 'a' words], 'e':[list of 'e' words], 'i': etc...}
  </code></pre>
  
  <p>Is there a way to do this with Python 2.7, 3+ dict comprehension? In other words, is it possible with the dict comprehension syntax to append the list represented by the key as the dict is being built?</p>
  
  <p>ie:</p>
  
  <pre><code>  index={k[0].lower():XXX for k in words if k[0].lower() in 'aeiou'}
  </code></pre>
  
  <p>Where XXX performs an append operation or list creation for the key as <code>index</code> is being created. </p>
  
  <p><strong>Edit</strong></p>
  
  <p>Taking the suggestions and benchmarking:</p>
  
  <pre><code>def f1():   
      d={}
      for word in words:
          c=word[0].lower()
          if c in 'aeiou':
             d.setdefault(c,[]).append(word)
  
  def f2():
     d={}
     {d.setdefault(word[0].lower(),[]).append(word) for word in words 
          if word[0].lower() in 'aeiou'} 
  
  def f3():
      d=defaultdict(list)                       
      {d[word[0].lower()].append(word) for word in words 
              if word[0].lower() in 'aeiou'}         
  
  def f4():
      d=functools.reduce(lambda d, w: d.setdefault(w[0], []).append(w[1]) or d,
         ((w[0].lower(), w) for w in words
          if w[0].lower() in 'aeiou'), {}) 
  
  def f5():   
      d=defaultdict(list)
      for word in words:
          c=word[0].lower() 
          if c in 'aeiou':
              d[c].append(word)       
  </code></pre>
  
  <p>Produces this benchmark:</p>
  
  <pre><code>   rate/sec    f4     f2     f1     f3     f5
  f4       11    -- -21.8% -31.1% -31.2% -41.2%
  f2       14 27.8%     -- -11.9% -12.1% -24.8%
  f1       16 45.1%  13.5%     --  -0.2% -14.7%
  f3       16 45.4%  13.8%   0.2%     -- -14.5%
  f5       18 70.0%  33.0%  17.2%  16.9%     --
  </code></pre>
  
  <p>The straight loop with a default dict is fastest followed by set comprehension and loop with <code>setdefault</code>.</p>
  
  <p>Thanks for the ideas!</p>