I was thinking you could initialize a dict with all the types and the counts set to 0. Then later when you display, just don't show the ones that have count 0. It's not like it's drastically better either, actually, so no need to change anything if you like this way better.

Here's another way that is a terrible one liner that you will hate and that I also haven't tried to make sure it actually works, but something like this could initialize the dictionary before that loop:

unit_count_by_type = dict([(type_id, 0) for type_id in set([u['type_id'] for u in copied_units])])

Of course, that just loops over the thing twice, so it's not like it's actually better.

This is another time that a dictionary comprehension would be great, but we don't have that. Yeah yeah, back in your day, you had to choose between except and finally…

Feel free or even inclined to ignore me.