-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictdiff.py
34 lines (28 loc) · 1007 Bytes
/
dictdiff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def dictdiff(first, second):
"""
This function takes two dicts as arguments and returns a new dict
that expresses the difference between the two dicts.
If there are no differences between the dicts, dictdiff returns an empty dict.
@param: dict - first, second
@return: dict
"""
# Initialize variable.
output = dict()
# Remove key duplicates before interation.
all_keys = first.keys() | second.keys()
for key in all_keys:
# Check if key values in first and second are unequal.
if first.get(key) != second.get(key):
# Add key value pair in output with value as a list
# containing key value in first and second dicts.
output[key] = [first.get(key), second.get(key)]
return output
# Test variables.
d1 = {'a': 1, 'b': 2, 'c': 3}
d2 = {'a': 1, 'b': 2, 'c': 4}
d3 = {'a': 1, 'b': 2, 'd': 3}
# Test calls.
print(dictdiff(d1, d1))
print(dictdiff(d1, d2))
print(dictdiff(d1, d3))
print(dictdiff(d2, d3))