Merge a dictionary | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Merge a dictionary

While merging 2 dicts into one, Why python picks a key(present in both dict) from second dict? #merge a dict pl1={'Ronaldo': 8, 'RVP':3} pl2={'RVP':4,'Messy':8} allplayers={**pl1,**pl2 } print(allplayers) #o/p {'Ronaldo':8, 'RVP':4, 'Messy':8} Why not RVP:3 in the output?

20th Apr 2019, 5:53 AM
Anil Patil
Anil Patil - avatar
2 Answers
+ 5
Because that's what you'd expect to happen. If you update an existing dictionary, older keys are overridden by newer keys. When merging two dictionaries, the keys of the first one are overridden by the second one
20th Apr 2019, 5:57 AM
Anna
Anna - avatar
+ 1
~ swim ~ Anna thank you
20th Apr 2019, 5:59 AM
Anil Patil
Anil Patil - avatar