I'm trying to figure out the difference in a dict, whether something was added or removed and from what.
Here is a case where a value is added:
original = {0: None, 1: False, 2: [16]}
new = {0: None, 1: False, 2: [2, 16]}
difference = True, {2: 2} # True = Added
And here is a case where a value is removed:
original = {0: None, 1: False, 2: [16, 64]}
new = {0: None, 1: False, 2: [64]}
difference = False, {2: 16} # False = Removed
The problem is that I have no idea how to recieve the difference. Would anyone happen to know how to achieve such a result?
Extra information (no idea if you'll need this):
- This can apply to 0 and 1 of original and new as well.
- 1 and 2 cannot be active at the same time. If one has values, the other is False.
That can be done in one line.
Here is a link to a function that can produce a "diff" of two dictionaries, followed by additional comments/code samples:
http://code.activestate.com/recipes/576644-diff-two-dictionaries/
Including code below:
What about something like:
you may temporarily transfer dic[2] to a
set
in python, and use-
to get the differenceAs I've explained in an other question there is a library on PyPI just for this task, which is datadiff library. It's easy to use and you can use the output to do what you have to do.
I find this quite readable: