Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Compare two nested dictionaries

3,337 views
Skip to first unread message

targetsmart

unread,
Jul 25, 2010, 11:03:06 AM7/25/10
to
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.

Steven D'Aprano

unread,
Jul 25, 2010, 12:21:14 PM7/25/10
to
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

News123

unread,
Jul 25, 2010, 12:30:30 PM7/25/10
to
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

Raymond Hettinger

unread,
Jul 25, 2010, 1:20:01 PM7/25/10
to

[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

John Nagle

unread,
Jul 26, 2010, 1:08:43 PM7/26/10
to
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

0 new messages