shorthand way to create dictionary key if it does not exist
python
Solution
def add_to_world(self, species, name, zone = 'retreat'):
self.object_attr.setdefault(species, {})[name] = {'zone' : zone}
Problem
I have a dictionary of zoo animals. I want to put it into the dictionary in a nested dictionary but get a KeyError because that particular species has not been added to the dictionary. ``` def add_to_world(self, species, name, zone = 'retreat'): self.object_attr[species][name] = {'zone' : zone} ``` Is there a shortcut to checking if that species is in the dictionary and create it if it is not or do i have to do it the long way and manually check if that species has been added?