title: Storing and updating lists in Python dictionaries: why does this happen?
body: <p>I have a list of data that looks like the following:</p>
  
  <pre><code>// timestep,x_position,y_position
  0,4,7
  0,2,7
  0,9,5
  0,6,7
  1,2,5
  1,4,7
  1,9,0
  1,6,8
  </code></pre>
  
  <p>... and I want to make this look like:</p>
  
  <pre><code>0, (4,7), (2,7), (9,5), (6,7)
  1, (2,5), (4,7), (9,0), (6.8)
  </code></pre>
  
  <p>My plan was to use a dictionary, where the value of t is the key for the dictionary, and the value against the key would be a list. I could then append each (x,y) to the list. Something like:</p>
  
  <pre><code># where t = 0, c = (4,7), d = {}
  
  # code 1
  d[t].append(c)
  </code></pre>
  
  <p>Now this causes IDLE to fail. However, if I do:</p>
  
  <pre><code># code 2
  d[t] = []
  d[t].append(c)
  </code></pre>
  
  <p>... this works. </p>
  
  <p>So the question is: why does code 2 work, but code 1 doesn't?</p>
  
  <p>PS Any improvement on what I'm planning on doing would be of great interest!! I think I will have to check the dictionary on each loop through the input to see if the dictionary key already exists, I guess by using something like max(d.keys()): if it is there, append data, if not create the empty list as the dictionary value, and then append data on the next loop through. </p>