Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to get difference of added datas between python dictionary?

Assuming,

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can do this with set.symmetric_difference():

set(*dict1.values()).symmetric_difference(*dict2.values())

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]}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading