Deep comparison of dicts - cmp() versus ==?

Victor Hooi victorhooi at gmail.com
Thu Mar 19 22:00:21 EDT 2015


Hi,

What is the currently most Pythonic way for doing deep comparisons between dicts?

For example, say you have the following two dictionaries

a = {
        'bob': { 'full_name': 'bob jones', 'age': 4, 'hobbies': ['hockey', 'tennis'], 'parents': { 'mother': 'mary', 'father', 'mike'}},
        'james': { 'full_name': 'james joyce', 'age': 6, 'hobbies': [],}
}

b = {
        'bob': { 'full_name': 'bob jones', 'age': 4, 'hobbies': ['hockey', 'tennis']},
        'james': { 'full_name': 'james joyce', 'age': 5, 'hobbies': []}
}

Previously, I though you could do a cmp():

cmp(a, b)

However, this page seems to imply that cmp() is deprecated?

https://docs.python.org/3/whatsnew/3.0.html#ordering-comparisons

Should we just be using the equality operator ("==") instead then? E.g.:

a == b

What is the reason for this?

Or is there a better way to do this?

Regards,
Victor



More information about the Python-list mailing list