<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def remove_entity(self, entity, second=False):
        '''
        Removes entity from world and kills entity
        '''
        if entity in self._entities:
            if second:
                for group in self._groups.keys():
                    if entity in self._groups[group]:
                        self.deregister_entity_from_group(entity, group)
                self._entities.remove(entity)
            else:
                entity.kill()
        else:
            raise UnmanagedEntityError(entity)