I made a dictionary subclass that will store duplicated values in lists under the same key automatically from reference to
Make a dictionary with duplicate keys in Python
class Dictlist(dict):
def __setitem__(self, key, value):
try:
self[key]
except KeyError:
super(Dictlist, self).__setitem__(key, [])
self[key].append(value)
d = Dictlist()
d['test'] = 1
d['test'] = 2
d['test'] = 1
d['test'] = 1
print(d)
{'test' : [1,2,1,1]}
for k,v in d.items():
d[k] = list(set(v))
print(d)
{'test': [1, 2, 1, 1, [1, 2]]}
In this case, the new list is being added to the values list. I just want [1,2]
in the values.