comparing dictionaries
Christophe Delord
christophe.delord at free.fr
Fri Aug 30 04:06:27 EDT 2002
Hi,
If you have a recent version of Python, you can use list comprehension.
For example:
>>> A = {1:'a', 2:'b', 4:'d', 5:'e'}
>>> B = {2:'B', 3:'C', 5:'e'}
Entries in A and not in B are:
>>> [ k for k in A if k not in B ]
[1, 4]
The same way, entries in B and not in A are:
>>> [ k for k in B if k not in A ]
[3]
Entries that have different values in A and B are:
>>> [ k for k in A if k in B and A[k]!=B[k]]
[2]
Christophe.
On Fri, 30 Aug 2002 09:38:29 +0200
Oliver Eichler <oliver.eichler at dspsolutions.de> wrote:
> Hi
>
> Is there an elegant way to compare dictionaries like:
>
> * What entries are in dictionary A and not in dictionary B
> * What entries are in dictionary B and not in dictionary A
> * What entries changed their values from A->B
>
>
> Oliver
--
(o_ Christophe Delord _o)
//\ http://christophe.delord.free.fr/ /\\
V_/_ mailto:christophe.delord at free.fr _\_V
More information about the Python-list
mailing list