python dictionary if key not exists add new code example
Example 1: python dictionary append value if key exists
from collections import defaultdict
d = defaultdict(list)
d['key'].append('mykey')
Example 2: add key if not exists python
# credit to Stack Overflow user in the source link
# d is your dictionary
if key not in d:
d[key] = value