# Compare two nested dictionaries

Discussion in 'Python' started by targetsmart, Jul 25, 2010.

1. ### targetsmartGuest

Hi,
I am trying to compare two nested dictionaries, I want to know what is
the exact difference between them. I tried this solution

....
s1 = set(result1)
s2 = set(result2)
print s1 - s2

but it doesn't seem show any difference, but

assert result1 == result2
fails

could someone help me to find out the difference the two nested
dictionaries.

Any help is greatly appreciated.

Thanks,
Vivek.

targetsmart, Jul 25, 2010

2. ### Steven D'ApranoGuest

On Sun, 25 Jul 2010 08:03:06 -0700, targetsmart wrote:

> Hi,
> I am trying to compare two nested dictionaries, I want to know what is
> the exact difference between them. I tried this solution
>
> ...
> s1 = set(result1)
> s2 = set(result2)
> print s1 - s2
>
> but it doesn't seem show any difference, but
>
> assert result1 == result2
> fails
>
> could someone help me to find out the difference the two nested
> dictionaries.

Have you tried printing them and just looking for the differences?

Calling set() on a dictionary will create a set from the keys only:

>>> d1 = {"a": 1, "b": 2}
>>> d2 = {"a": 1, "b": 999}
>>> set(d1) == set(d2)

True
>>> d1 == d2

False

If you want to know the difference between two dictionaries, you have to
consider:

(1) Keys that are in the first dict, but not the second;

(2) Keys that are in the second dict, but not the first; and

(3) Keys which are in both dicts, but have different values.

--
Steven

Steven D'Aprano, Jul 25, 2010

3. ### News123Guest

Hi,

On 07/25/2010 06:21 PM, Steven D'Aprano wrote:
> On Sun, 25 Jul 2010 08:03:06 -0700, targetsmart wrote:
>
>> Hi,
>> I am trying to compare two nested dictionaries, I want to know what is
>> the exact difference between them. I tried this solution
>>
>> ...
>> s1 = set(result1)
>> s2 = set(result2)
>> print s1 - s2
>>

I think you want to have the symmetric difference:
try s1 ^ s2

>> but it doesn't seem show any difference, but
>>

News123, Jul 25, 2010
4. ### Raymond HettingerGuest

[targetsmart]
> > I am trying to compare two nested dictionaries, I want to know what is
> > the exact difference between them. I tried this solution

[Steven D'Aprano]
> If you want to know the difference between two dictionaries, you have to
> consider:
>
> (1) Keys that are in the first dict, but not the second;
>
> (2) Keys that are in the second dict, but not the first; and
>
> (3) Keys which are in both dicts, but have different values.

Steven, thanks for the excellent specification. Here's the code:

s1 = set(d1)
s2 = set(d2)
first_not_second = s1 - s2
second_not_first = s2 - s1
difference_values = set(k for k in s1 & s2 if d1[k] != d2[k])

If the values are hashable, an alternate approach is:

s1 = set(d1.items())
s2 = set(d2.items())
first_not_second = s1 - s2
second_not_first = s2 - s1

Raymond

Raymond Hettinger, Jul 25, 2010
5. ### John NagleGuest

On 7/25/2010 8:03 AM, targetsmart wrote:
> Hi,
> I am trying to compare two nested dictionaries, I want to know what is
> the exact difference between them.

d1 = {'a' : 1, 'b' : 2, 'c': 3 }
d2 = {'a' : 1, 'b' : 3, 'd': 4 }

diff = dict(set(d1.items()) - set(d2.items()))

print (diff)

{'c': 3, 'b': 2}

That's the true "difference", with all entries in d1 not
identically in d2 listed. Is that what you wanted?

John Nagle

John Nagle, Jul 26, 2010