# Comparing 2 nested dictionaries and print the difference

**URL:** <https://forum.ansible.com/t/comparing-2-nested-dictionaries-and-print-the-difference/34317>\
**Category:** Ansible Project\
**Created:** [December 10, 2020, 7:14pm UTC](https://forum.ansible.com/t/comparing-2-nested-dictionaries-and-print-the-difference/34317 "2020-12-10T19:14:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Umesha\_A](https://avatars.discourse-cdn.com/v4/letter/u/59ef9b/32.png) [@Umesha\_A](https://forum.ansible.com/u/Umesha_A)\
**Post date:** [December 10, 2020, 7:14pm UTC](https://forum.ansible.com/t/comparing-2-nested-dictionaries-and-print-the-difference/34317/1 "2020-12-10T19:14:05Z")

</div>

HI  
I have 2 nested dictionaries to compare and print their result. How can this be done ? Any thoughts ? I want to check if there are any databases and tables are missing or additonal tables are present by comparing the following 2 nested dictionaries.

expected\_dbs:  
‘database1’:  
db\_tables:

- “non-existant-table”
- “table1\_db1”
- “table2\_db2”
- “table3\_db3”

‘database2’:  
db\_tables:

- “non-existant-table”
- “table1\_db2”
- “table2\_db2”
- “table3\_db2”  
‘database3’:  
db\_tables:
- “non-existant-table”
- “table1\_db3”
- “table2\_db3”
- “table3\_db3”

actual\_dbs:  
‘database1’:  
db\_tables:

- “table1\_db1”
- “table2\_db2”
- “table3\_db3”

‘database2’:  
db\_tables:

- “table1\_db2”
- “table2\_db2”
- “table3\_db2”
- “additional\_table”

‘database3’:  
db\_tables:

- “table1\_db3”
- “table2\_db3”

I could iterate over one database at a time and then compare, but was thinking if there is any better way to do it ?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 10, 2020, 8:22pm UTC](https://forum.ansible.com/t/comparing-2-nested-dictionaries-and-print-the-difference/34317/2 "2020-12-10T20:22:35Z")

</div>

This is a simple difference of (expected - actual) with common set of  
the keys

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;diff\_dbs: "{{ diff\_dbs|default({})|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;combine({item: diff}) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ expected\_dbs|intersect(actual\_dbs) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;diff: "{{ expected\_dbs[item]['db\_tables']|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;difference(actual\_dbs[item]['db\_tables']) }}"

There are other options, e.g. see "symmetric\_difference"  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters.html#selecting-from-sets-or-lists-set-theory](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#selecting-from-sets-or-lists-set-theory)

---

<div class="post-metadata">

**Author:** ![Umesha\_A](https://avatars.discourse-cdn.com/v4/letter/u/59ef9b/32.png) [@Umesha\_A](https://forum.ansible.com/u/Umesha_A)\
**Post date:** [December 10, 2020, 9:22pm UTC](https://forum.ansible.com/t/comparing-2-nested-dictionaries-and-print-the-difference/34317/3 "2020-12-10T21:22:10Z")

</div>

Thank you so much.
