New course! Every coder should learn Generative AI!
Try a free lesson+ 2
Write a program to Merge two Python dictionaries into one [Solved]
https://code.sololearn.com/c28ry6Avtmv9/?ref=app In this I have a doubt that if dict1 and dict2 both have 30 in them then why after merging them there is only one time 30 written. Please help
1 Answer
+ 2
In Python, dictionary keys must all be unique with one another.
Here the dict1 and dict2 merge or concatenate.
Now the dictionary will look like this:
dict3 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30, 'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
But since dictionary keys must be unique, the first "Thirty" key (which is not a unique key), should be removed.
Therefore, the dict3 will look now like this:
dict3 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
- - - - - - - - - - - - - - - - - -
Try this and the value of 'Thirty' would be 25 because it gets the last or the last declared similar key.
dict3 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30, 'Thirty': 25, 'Fourty': 40, 'Fifty': 50}
duct3 = {'Ten': 10, 'Twenty': 20, 'Thirty': 25, 'Fourty': 40, 'Fifty': 50}