title: Is the defaultdict in Python's collections module really faster than using setdefault?
body: <p>I've seen other Python programmers use defaultdict from the collections module for the following use case:</p>
  
  <pre><code>from collections import defaultdict
  
  s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
  
  def main():
      d = defaultdict(list)
      for k, v in s:
          d[k].append(v)
  </code></pre>
  
  <p>I've typically approached this problem by using setdefault instead:</p>
  
  <pre><code>def main():
      d = {}
      for k, v in s:
          d.setdefault(k, []).append(v)
  </code></pre>
  
  <p>The docs do in fact <a href="http://docs.python.org/library/collections.html#collections.defaultdict" rel="noreferrer">claim that using defaultdict is faster</a>, but I've seen the opposite to be true when testing myself:</p>
  
  <pre><code>$ python -mtimeit -s "from withsetdefault import main; s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)];" "main()"
  100000 loops, best of 3: 4.51 usec per loop
  $ python -mtimeit -s "from withdefaultdict import main; s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)];" "main()"
  100000 loops, best of 3: 5.38 usec per loop
  </code></pre>
  
  <p>Is there something wrong with how I've set up the tests?</p>
  
  <p>For reference, I'm using Python 2.7.3 [GCC 4.2.1 (Apple Inc. build 5666)</p>