How could dictionaries whose values are lists be merged in Python, so that all of the keys are moved into one dictionary, and all the elements of each list moved into a single list for each key?
For example, with these dictionaries:
x = {'a': [2], 'b': [2]}
y = {'b': [11], 'c': [11]}
... the result of the merging should be like this:
{'a': [2], 'b': [2, 11], 'c': [11]}
How could this be done with any number of dictionaries, not just two?
for k, v in y.items():
x.setdefault(k, []).extend(v)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With