I have this kind of dictionary of lists, initially empty:
d = dict()
Now the use case is to simply add a value to list under a key, which might be a new or an existing key. I think we have to do it like this:
if key not in d:
d[key] = list()
d[key].append(value)
This seems awfully tedious and error-prone, needing to write multiple lines (with copy-paste or helper function). Is there a more convenient way?
If there isn't "only one way to do it", and you know it, you can also answer that, and maybe suggest alternative ways to accomplish above, even if they aren't necessarily better.
I looked for duplicate, didn't find, but perhaps I just didn't know to use right keywords.
.setdefaultmethod for doing this sort of thing.