Home > database >  How to get root using DeepDiff
How to get root using DeepDiff

Time:02-17

I'm currently working with DeepDiff where I'm trying to figure out how I can get the value of the root instead of printing out the root e.g.

Value of root['hello'][0]['hello_world'] changed from "15251" to "51251".

So I have made a simple script

from deepdiff import DeepDiff

dict_a = {'hello': [{'what': 'uh', 'hello_world': '15251'}]}
dict_b = {'hello': [{'hello_world': '51251', 'what': 'uh'}]}

t = DeepDiff(dict_a, dict_b, ignore_order=True)
print(t.pretty())

>>> Value of root['hello'][0]['hello_world'] changed from "15251" to "51251".

and the output that I want to do is that I want it to be able to print out root['hello'][0] so in our case that would be dict_b['hello'][0] >>> {'hello_world': '51251', 'what': 'uh'} so that I can easily track in my dict_b the whole list rather than just the value

Is it possible to do this using DeepDiff?

CodePudding user response:

You can use the tree-view option by providing view="tree" as argument. Then you have up and t2 properties to navigate to the parent at the side of the second input structure:

t = DeepDiff(dict_a, dict_b, ignore_order=True, view="tree")
for change in t["values_changed"]:
    print(change.up.t2)  # {'hello_world': '51251', 'what': 'uh'}
  • Related