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?

Alex
  • 31
  • 4
  • If your dict's values are [hashable](https://docs.python.org/3/glossary.html#term-hashable) then you can implement it easily by converting dict into tuples and using `set` [like this answer](https://stackoverflow.com/a/41808831/10909029) – jupiterbjy Jun 12 '22 at 07:05
  • Here's [demo code](https://gist.github.com/jupiterbjy/6d84bbe80b5279ed0695fc67ee176305) based on my understanding of what you're trying to achieve, feel free to check out if you find yourself lost. – jupiterbjy Jun 12 '22 at 08:25

0 Answers0