how to get difference of added datas between python dictionary?



dict1 = { "A" : ["a","b","c"]} # old one
dict2 = { "A" : ["a","b","c","d"]} # new one

#i wanna get
dict3 = {"A":["d"]}

as i know, taking up ‘deepdiif’ isn’t right because it has to have same architecture of Dictionary
how can i get that diffrence of added datas of Dictionary in simple and easy way?

>Solution :

You can do this with set.symmetric_difference():


If you have more dictionary keys than just one:

In [2]: dict1
Out[2]: {'A': ['a', 'b', 'c'], 'B': [1, 2, 3, 4]}

In [3]: dict2
Out[3]: {'A': ['a', 'b', 'c', 'd'], 'B': [4, 3, 2]}

In [4]: {k: list(set(dict1[k]).symmetric_difference(dict2[k])) for k in dict1}
Out[4]: {'A': ['d'], 'B': [1]}

Leave a ReplyCancel reply