Question: Write a Python function merge _ dictionaries that takes two dictionaries and returns a new dictionary containing all the keys from both dictionaries. If a

Write a Python function merge_dictionaries that takes two dictionaries and returns a new dictionary containing all the keys from both dictionaries.
If a key exists in both dictionaries, the value in the new dictionary should be a list containing both values.
Test: merge_dictionaries({'a': 1,'b': 2},{'b': 3,'c': 4}) should return {'a': 1,'b': [2,3],'c': 4}.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!