# Compare two dictionaries on keys and return keys and items

**URL:** <https://forum.inductiveautomation.com/t/compare-two-dictionaries-on-keys-and-return-keys-and-items/51115>\
**Category:** Ignition\
**Tags:** scripting, vision, ignition81\
**Created:** [September 19, 2021, 6:02pm UTC](https://forum.inductiveautomation.com/t/compare-two-dictionaries-on-keys-and-return-keys-and-items/51115 "2021-09-19T18:02:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [September 19, 2021, 6:02pm UTC](https://forum.inductiveautomation.com/t/compare-two-dictionaries-on-keys-and-return-keys-and-items/51115/1 "2021-09-19T18:02:09Z")

</div>

If I have two dictionaries:

```auto
{MyKey1 : MyValue1, MyKey2 : MyValue2, MyKey3 : MyValue3, MyKey4 : MyValue4}
{MyKey1 : MyValue1, MyKey2 : MyValue2}

```

I want to compare for keys, not values, and return the differences as a dictionary, in this case:

```auto
{MyKey3 : MyValue3, MyKey4 : MyValue4}

```

I know I can loop through, but was wondering if there is a more pythonic way. The reason I want to use keys, is they will always be the same across dictionaries, the values might not.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [September 19, 2021, 7:01pm UTC](https://forum.inductiveautomation.com/t/compare-two-dictionaries-on-keys-and-return-keys-and-items/51115/2 "2021-09-19T19:01:40Z")

</div>

It sort of depends on what your expectations are.

Given the following…

```auto
my_dict = {'One': 1, 'Two': 2}
my_dict_two = {'One': 'one', 'Three': 3}

```

Then the following code would get you the keys which are in `my_dict_two`, but which are not in `my_dict`.

```auto
list(set(my_dict_two) - set(my_dict))
['Three']

```

This is only half of the available information, however, because there are keys in `my_dict` which are not in `my_dict_two`. If you want to get all keys which are not accounted for in both dictionaries, then you need to include the “other” direction:

```auto
list(set(my_dict_two) - set(my_dict)) + list(set(my_dict) - set(my_dict_two))
['Three', 'Two']

```

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [September 19, 2021, 7:07pm UTC](https://forum.inductiveautomation.com/t/compare-two-dictionaries-on-keys-and-return-keys-and-items/51115/3 "2021-09-19T19:07:36Z")

</div>

Is this pythonic enough? 🙂

```auto
diff = { k : mainDict[k] for k in set(mainDict) - set(secDict) }

```

I only need to compare one way, main to secondary. The keys will always be the same, since the secondary dictionary is derived from the main. I was having issues finding a good way to have a result as a dictionary with both keys and values.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [September 20, 2021, 12:24am UTC](https://forum.inductiveautomation.com/t/compare-two-dictionaries-on-keys-and-return-keys-and-items/51115/4 "2021-09-20T00:24:09Z")

</div>

Yes, but if all you’re interested in is the difference in keys, do you need the values as well? It seems like  
you’re setting up a dictionary of differences WITH the values when you already said you just needed to know about the missing keys. If you need the differences and their values, then what you supplied should work and is better than looping over the dictionaries.

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [September 20, 2021, 12:59pm UTC](https://forum.inductiveautomation.com/t/compare-two-dictionaries-on-keys-and-return-keys-and-items/51115/5 "2021-09-20T12:59:28Z")

</div>

> [@cmallonee](#):
>
> Yes, but if all you're interested in is the difference in keys, do you need the values as well?

I want the compare on keys, but return both as a dictionary. I tested and what I posted seems to do the trick.
