Application of Removing Items from Dictionaries
<p>Suppose I have a dictionary that contains instances of kinematic objects. Each kinematic object has a position, velocity, etc. On each timestep update for the program, I want to check to see if two active objects (not the same object, mind you) occupy the same position in the reference frame. If they do, this would simulate a collision, the two objects involved would be destroyed, and their instances would be removed from the active objects dictionary.</p>

<pre><code>dict actives{ 'missile' : object_c(x1, y1, z1),
              'target'  : object_c(x2, y2, z2),
              'clutter' : object_c(x3, y3, z3),
              ...                               }

...

for key1 in self.actives.keys():
   for key2 in self.actives.keys():
      if not key1 == key2:
         # Get Inertial Positions and Distance
         Pos21 = self.actives[key2].Pos - self.actives[key1].Pos
         distance = math.sqrt(sum(Pos21**2))
         # If Distance &lt;= Critical Distance
         if distance &lt;= 1.0e0
            # Remove key1 and key2 from Actives
            # -- This is where I need help --
</code></pre>

<p>I can't use <code>del</code>:  the keys (and objects) would be removed from actives, but the <code>for</code> loops' conditions fail to recognize this and will encounter a KeyError. What can I do to remove these objects from actives while accessing the keys for the loop conditions?</p>

<p>I think Maximilian Peters had the right basic idea, but the items to be removed should be kept in a <code>set</code> rather than a <code>list</code> to avoid issues with an active key being in it multiple times. To further hasten the collision detection process, I changed the comparison loop to use the <a href="https://docs.python.org/2/library/itertools.html#itertools.combinations" rel="nofollow"><code>itertools.combinations()</code></a> generator function so as to only test unique pairs of objects.</p>

<p>I also had to add a fair amount of scaffolding to make it possible test the code in a context like you probably have it running in...</p>

<pre><code>from itertools import combinations
import math

CRITICAL_DIST = 2.0e0

class ObjectC(object):
    def __init__(self, x, y, z):
        self.posn = x, y, z
    def __repr__(self):
        return '{}({}, {}, {})'.format(self.__class__.__name__, *self.posn)

class Game(object):
    def remove_collisons(self):
        to_remove = set()
        for key1, key2 in combinations(self.actives, 2):
             # Calculate distance.
             deltas = (
                 (self.actives[key2].posn[0] - self.actives[key1].posn[0])**2,
                 (self.actives[key2].posn[1] - self.actives[key1].posn[1])**2,
                 (self.actives[key2].posn[2] - self.actives[key1].posn[2])**2)
             distance = math.sqrt(sum(deltas))
             # Check for collision.
             if distance &lt;= CRITICAL_DIST:
                to_remove |= {key1, key2}  # both objects should be removed

        if to_remove:
            print('removing: {!r}'.format(list(to_remove)))
            self.actives = {
                k: v for k, v in self.actives.items() if k not in to_remove}

x1, y1, z1 = 0, 1, 2
x2, y2, z2 = 1, 2, 3
x3, y3, z3 = 2, 3, 1

actives = {'missile' : ObjectC(x1, y1, z1),
           'target'  : ObjectC(x2, y2, z2),
           'clutter' : ObjectC(x3, y3, z3),
          } # ...

game = Game()
game.actives = actives
print('before: {}'.format(game.actives))
game.remove_collisons()
print('after: {}'.format(game.actives))
</code></pre>

<p>Output:</p>

<pre class="lang-none prettyprint-override"><code> before: {'clutter': ObjectC(2, 3, 1), 'target': ObjectC(1, 2, 3), 'missile': ObjectC(0, 1, 2)}
 removing: ['target', 'missile']
 after: {'clutter': ObjectC(2, 3, 1)}
</code></pre>

python dictionary