In Python, when I merge two dictionaries using the update() method, any existing keys will be overwritten.
Is there a way to merge the two dictionaries while keeping the original keys in the merged result?
Say we had the following example:
dict1 = {'bookA': 1, 'bookB': 2, 'bookC': 3}
dict2 = {'bookC': 2, 'bookD': 4, 'bookE': 5}
Can we merge the two dictionaries, such that the result will keep both values for the key bookC
?
I'd like dict3
to look like this:
{'bookA': 1, 'bookB': 2, 'bookC': (2,3), 'bookD': 4, 'bookE': 5}