Detecting changes to a dict

CTO debatem1 at
Mon Sep 28 06:42:10 CEST 2009

On Sep 27, 5:36 am, Steven D'Aprano <st... at REMOVE-THIS-> wrote:
> I'm pretty sure the answer to this is No, but I thought I'd ask just in
> case...
> Is there a fast way to see that a dict has been modified? I don't care
> what the modifications are, I just want to know if it has been changed,
> where "changed" means a key has been added, or deleted, or a value has
> been set. (Modifications to mutable values aren't important.) In other
> words, any of these methods count as modifying the dict:
> __setitem__
> __delitem__
> clear
> pop
> popitem
> setdefault
> update
> Of course I can subclass dict to do this, but if there's an existing way,
> that would be better.
> --
> Steven

d = {"a": "b", "c": "d"}
d2 = d.copy()
assert d == d2
d["e"] = "f"
assert d == d2

Is this what you're looking for?

Geremy Condra

More information about the Python-list mailing list