title: How does collections.defaultdict work?
body: <p>I've read the examples in python docs, but still can't figure out what this method means. Can somebody help? Here are two examples from the python docs</p>
  
  <pre><code>&gt;&gt;&gt; from collections import defaultdict
  
  &gt;&gt;&gt; s = 'mississippi'
  &gt;&gt;&gt; d = defaultdict(int)
  &gt;&gt;&gt; for k in s:
  ...     d[k] += 1
  ...
  &gt;&gt;&gt; d.items()
  [('i', 4), ('p', 2), ('s', 4), ('m', 1)]
  </code></pre>
  
  <p>and</p>
  
  <pre><code>&gt;&gt;&gt; s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
  &gt;&gt;&gt; d = defaultdict(list)
  &gt;&gt;&gt; for k, v in s:
  ...     d[k].append(v)
  ...
  &gt;&gt;&gt; d.items()
  [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
  </code></pre>
  
  <p>the parameters <code>int</code> and <code>list</code> are for what? </p>