title: Python dict.setdefault uses more memory?
body: <p>I was writing some Python code that involved something like this</p>
  
  <pre><code>values = {}
  for element in iterable:
      values.setdefault(element.name, []).append(element)
  </code></pre>
  
  <p>Because I could have sorted the input previously, I also implemented it like this</p>
  
  <pre><code>values = {}
  
  cur_name = None
  cur_list = None
  
  for element in iterable:
      if element.name != cur_name:
          values[cur_name] = cur_list
          cur_name = element.name
          cur_list = []
      cur_list.append(element)
  if cur_list:
      values[cur_name] = cur_list
  del values[None]
  </code></pre>
  
  <p>Here the input is already sorted by <code>element.name</code>.</p>
  
  <p>The second approach was much faster than the first approach, and it also used less memory.</p>
  
  <p>What's the reason for this?</p>
  
  <p>Or have I made some sort of mistake in the second approach?</p>