<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _clear_dead_entities(self):
 """Finalize deletion of any Entities that are marked dead. In the interest of performance, this method duplicates code from the `delete_entity` method. If that method is changed, those changes should be duplicated here as well. """

        for entity in self._dead_entities:

            for component_type in self._entities[entity]:
                self._components[component_type].discard(entity)

                if not self._components[component_type]:
                    del self._components[component_type]

            del self._entities[entity]

        self._dead_entities.clear()
        self.clear_cache()