Q:

Merge two Python dictionaries into one

belongs to collection: Python Dictionary Exercises

0

Merge two Python dictionaries into one

dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}

Expected output:

{'Ten': 10, 'Twenty': 20, 'Thirty': 30, 'Fourty': 40, 'Fifty': 50}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Solution:

Python 3.5+

dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}

dict3 = {**dict1, **dict2}
print(dict3)

Other Version:

dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}

dict3 = dict1.copy()
dict3.update(dict2)
print(dict3)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Print the value of key ‘history’ from the belo... >>
<< Convert two lists into a dictionary using python p...