Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check two dictionaries that have similar keys but different values

I have two dictionaries. dict1 and dict2. dict 2 is always of the same length but dict1 varies in length. Both dictionaries are as follows:

dict2 = {"name":"martin","sex":"male","age":"97","address":"blablabla"}

dict1 = {"name":"falak", "sex":"female"}

I want to create a third dictionary that is based on both dict1 and dict2. dict3 will have all values of dict2. But all those keys will be replaced that exists in dict1. Here is the resulting dict3

dict3 = {"name":"falak","sex":"female","age":"97","address":"blablabla"}

I can do it wil multiple if statements but want to have a way that is more smarter. Can please someone guide me regarding that.

like image 781
hjelpmig Avatar asked Sep 01 '25 17:09

hjelpmig


1 Answers

Have you tried:

dict3 = dict(dict2, **dict1)

Or:

dict3 = dict2.copy()
dict3.update(dict1)
like image 160
Jon Clements Avatar answered Sep 04 '25 05:09

Jon Clements