This isn't likely to be a problem at our scale, but we could be more efficient than iterating over the entire tag list once for each unit.  How about something like:
```
unit_ids = set([unit.id for unit in units])
for tag_key, tag_image_id in tags.items():
  if tag_image_id in unit_ids:
    del tags[tag_key]
```

Then ``units`` and ``tags`` get scanned only once each.