Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #87754

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

From Ben Finney <ben+python@benfinney.id.au>
Subject Re: Deep comparison of dicts - cmp() versus ==?
Date 2015-03-20 13:33 +1100
References <d88b9040-b566-48fc-89e9-c6dffcd369bd@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.31.1426818822.10327.python-list@python.org> (permalink)

Show all headers | View raw


Victor Hooi <victorhooi@gmail.com> writes:

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

What distinction do you intend by saying “deep comparison”? As
contrasted with what?

> 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': []}
> }

Those two dicts are not equal. How would your intended “deep comparison”
behave for those two values?

> However, this page seems to imply that cmp() is deprecated?
> https://docs.python.org/3/whatsnew/3.0.html#ordering-comparisons

It is, yes.

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

Yes. That is a comparison that would return False for comparing the
above two values. Would you expect different behaviour?

> What is the reason for this?

I don't really understand. ‘cmp’ is deprecated, and you can compare two
dicts with the built-in operators. That's the reason; are you expecting
some other reason?

> Or is there a better way to do this?

I don't really know what it is you want to do. What behaviour different
from the built-in comparison operators do you want?

-- 
 \         “I went over to the neighbor's and asked to borrow a cup of |
  `\       salt. ‘What are you making?’ ‘A salt lick.’” —Steven Wright |
_o__)                                                                  |
Ben Finney

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Deep comparison of dicts - cmp() versus ==? Victor Hooi <victorhooi@gmail.com> - 2015-03-19 19:00 -0700
  Re: Deep comparison of dicts - cmp() versus ==? Ben Finney <ben+python@benfinney.id.au> - 2015-03-20 13:33 +1100
    Re: Deep comparison of dicts - cmp() versus ==? Victor Hooi <victorhooi@gmail.com> - 2015-03-19 19:57 -0700
      Re: Deep comparison of dicts - cmp() versus ==? Rustom Mody <rustompmody@gmail.com> - 2015-03-19 20:27 -0700
        Re: Deep comparison of dicts - cmp() versus ==? Chris Angelico <rosuav@gmail.com> - 2015-03-20 14:34 +1100
          Re: Deep comparison of dicts - cmp() versus ==? Rustom Mody <rustompmody@gmail.com> - 2015-03-19 20:47 -0700
            Re: Deep comparison of dicts - cmp() versus ==? Chris Angelico <rosuav@gmail.com> - 2015-03-20 15:35 +1100
            Re: Deep comparison of dicts - cmp() versus ==? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-20 17:35 +1100
              Re: Deep comparison of dicts - cmp() versus ==? Roy Smith <roy@panix.com> - 2015-03-20 08:49 -0400
              Re: Deep comparison of dicts - cmp() versus ==? Rustom Mody <rustompmody@gmail.com> - 2015-03-20 06:46 -0700
        Re: Deep comparison of dicts - cmp() versus ==? Ben Finney <ben+python@benfinney.id.au> - 2015-03-20 16:09 +1100
          Re: Deep comparison of dicts - cmp() versus ==? Rustom Mody <rustompmody@gmail.com> - 2015-03-20 06:35 -0700
            Re: Deep comparison of dicts - cmp() versus ==? Marko Rauhamaa <marko@pacujo.net> - 2015-03-20 16:10 +0200
              OT Intuitionism+Platonism+Mysticism => CS (was Deep comparison of dicts...) Rustom Mody <rustompmody@gmail.com> - 2015-03-20 23:01 -0700
                Re: OT Intuitionism+Platonism+Mysticism => CS (was Deep comparison of dicts...) Marko Rauhamaa <marko@pacujo.net> - 2015-03-21 08:28 +0200
                Re: OT Intuitionism+Platonism+Mysticism => CS (was Deep comparison of dicts...) Rustom Mody <rustompmody@gmail.com> - 2015-03-31 20:53 -0700
        Re: Deep comparison of dicts - cmp() versus ==? Chris Angelico <rosuav@gmail.com> - 2015-03-20 16:27 +1100

csiph-web