Topics
The defaultdict
class from the collections built-in module automatically stores a default value when a key doesn’t exist. All you have to do is provide a function that will return the default value to use each time a key is missing:
from collections import defaultdict
class Visits:
def __init__(self):
self.data = defaultdict(set)
def add(self, country, city):
self.data[country].add(city)
visits = Visits()
visits.add("England", "Bath")
visits.add("England", "London")
Using defaultdict
is much better than using setdefault
for this type of situation