0

I Have two dictionary and i want to compare them to understand which item added or deleted or modified it's value.

I write a class for doing compare two dict.

class CompareTwoDict:
    def __init__(self, dict1, dict2) -> None:
        self._dict1 = dict1
        self._dict2 = dict2
    
    def added_from_dict1(self):
        ...
    def deleted_from_dict1(self):
        ...
    def modified_from_dict1(self):
        ...
    def common_between_dict1_dict2(self):
        ...

added_from_dict1 means any item in dict2 that is not in dict1.

deleted_from_dict1 means any item in dict1 but it is not in dict2.

modified_from_dict1 means item's value change in dict2.

common_between_dict1_dict2 means items in dict1 and dict2 have the same key and value.

Is There any python standard function do it?

2
  • If your dict's values are hashable then you can implement it easily by converting dict into tuples and using set like this answer
    – jupiterbjy
    Jun 12, 2022 at 7:05
  • Here's demo code based on my understanding of what you're trying to achieve, feel free to check out if you find yourself lost.
    – jupiterbjy
    Jun 12, 2022 at 8:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.