title: adding / overwriting to dictionary depending on elements
body: <p>py 2.7. I have a dictionary of lists. Each cycle each list is overwritten with a new updated version of itself.</p>
  
  <p>I'm using a third party particle system. What I'm doing is having each key as the particle's index number and the list is of it's position and previous positions. </p>
  
  <p>But when a particle 'dies', the indexes of the particles all get shifted. A key will be overwritten incorrectly when a new particle is born with the same index as the dead particle. I want to preserve that key with positions of the dead particle.</p>
  
  <p>Here's the code as it is now:</p>
  
  <pre><code>if frame == 0:
      branches = {}
  
  ...
  
  for p in xrange(particle_count):
      xp = emitter.GetParticle(p) #xp = current particle
      trail = []
      index = xp.GetIndex()  
      trail_length = xp.GetCustomDataCount() #number of previous positions
  
      for i in xrange(trail_length):
          previous_position = xp.GetCustomData(i)
          trail.append(previous_position)
          branches [index] = trail
  </code></pre>
  
  <p>I was thinking of comparing the first element of each list with the first element of the list that it's trying to overwrite. Then if it's different, add 1 to the index number until there's a free spot...?</p>
  
  <p>EDIT - I've made further progress and have identified what I need to do, but don't know the python. Here's some new code:</p>
  
  <pre><code>for p in xrange(particle_count):
      xp = emitter.GetParticle(p) #xp = current particle
      trail = []
      index = xp.GetIndex()  
      trail_length = xp.GetCustomDataCount()
  
      for i in xrange(trail_length):
          previous_position = xp.GetCustomData(i)
          trail.append(previous_position)
  
      if index in branches:
          this_trail = trail[0]
              set_trail = branches[index]
              set_trail = set_trail[0]
  
              if this_trail == set_trail:
                  branches[index] = trail
              else:
                  for b in branches:
                      set_trail = branches[b]
                      set_trail = set_trail[0]
                      if this_trail == set_trail:
                          branches[index] = trail
                          break
       else:
           branches[index] = trail
  </code></pre>
  
  <p>The problem: When I say "if index in branches.." I'm checking each entry for a match. If the trails are the same, the old is overwritten with the new. However, if the index does exist in the dictionary, but is not the same as the entry, nothing will happen. Here's what I need:</p>
  
  <pre><code>if index in branches:
      this_trail = trail[0]
      set_trail = branches[index]
      set_trail = set_trail[0]
  
      if this_trail == set_trail:
          branches[index] = trail
      else:
          check all entries for a match(like i currently do)
          if match, overwrite entry
          if no match, add entry to a non-existing key
  else:
      branches[index] = trail
  </code></pre>