I want to automatically add keys to a Python dictionary if they don’t exist already.
For example,
a = "a"
b = "b"
c = "c"
dict = {}
dict[a][b] = c # doesn't work because dict[a] doesn't exist
How do I automatically create keys if they don’t exist?
Use a
collections.defaultdict: