Compare two nested dictionaries
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sun Jul 25 12:21:14 EDT 2010
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
More information about the Python-list
mailing list