This is the whole point of defaultdict: you construct one with a callable (in this case, int
), and when a key is accessed that doesn’t exist, it will call it (producing 0
), and insert it into the dictionary. It auto-creates keys as you access them.
solved Python adding elements to dict and to defaultdict [closed]