-3

Solution for Compare two dict and update the dict based on compared dict

compare dict1 with dict2 and update the dict1 based on the dict key values,here "id" is the unique key

dict1={id:1,mobile:'nokia',count:54,size:9}

dict2={id:1,mobile:'nokia',count:63,size:9}

expected output:

dict1={id:1,mobile:'nokia',count:63,size:9}
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 1
    What should the comparison do? This is probably just `dict1.update(dict2)`. – RemcoGerlich Apr 05 '19 at 10:04
  • Before comparing these dict,Need to iterate the the "id" which means check the "dict1" id= "dict" id and update the "dict1" values from "dict2" – Vivek M Vijay Apr 05 '19 at 10:09
  • Possible duplicate of [How to merge two dictionaries in a single expression?](https://stackoverflow.com/questions/38987/how-to-merge-two-dictionaries-in-a-single-expression) – Jondiedoop Apr 05 '19 at 10:10

1 Answers1

0

Sample Code:

dict1={"id":1,"mobile":'nokia',"count":54,"size":9}

dict2={"id":1,"mobile":'nokia',"count":63,"size":9}

dict1.update(dict2)

print(dict1)

Output:

{'id': 1, 'mobile': 'nokia', 'count': 63, 'size': 9}
Anonymous
  • 659
  • 6
  • 16