title: Append to defaultdict(list) based upon items in an iterable
body: <p>I have a program that is supposed to apply a function to an iterable and return
  a dict(list) in which the keys are the values from the function and the values in the list are the items that produced the value from the function.</p>
  
  <p>Code:</p>
  
  <pre><code>from collections import defaultdict
  
  
  numbers = [1, 4, 5, 6, 8, 19, 34, 55]
  d = defaultdict(list)
  
  
  def modn(n):
      return n % 3
  
  
  def group_by_value(it, func):
      result = map(func, it)
  
      for i in result:
          for j in it:
              d[i].append(j)
  
      return d
  
  
  print(group_by_value(numbers, modn))
  </code></pre>
  
  <p>I am getting:
  {1: [1, 4, 5, 6, 8, 19, 34, 55, 1, 4, 5, 6, 8, 19, 34, 55, 1, 4, 5, 6, 8, 19, 34, 55, 1, 4, 5, 6, 8, 19, 34, 55, 1, 4, 5, 6, 8, 19, 34, 55], 2: [1, 4, 5, 6, 8, 19, 34, 55, 1, 4, 5, 6, 8, 19, 34, 55], 0: [1, 4, 5, 6, 8, 19, 34, 55]}</p>
  
  <p>I should get:</p>
  
  <p>{0: [6], 1: [1, 4, 19, 34, 55], 2: [5, 8]}</p>
  
  <p>I understand why I am getting the wrong result.  However, I keep wanting to run the modn() function twice:  once to get the result, and once to do a comparison to see if the item in numbers is already there.  I can't help but think I should not have to run that function twice, but I am stuck.  </p>
  
  <p>I should be able to apply the function to each item in the iterable, get the return value, then populate the list with the items from the iterable that produced the value, without running the function twice.</p>