-2

What does the == operator actually do on a Python dictionary?

From the question above , == operator will compare two dictionaries keys and their corresponding values. But I see in many places in code where the keys and values are iterated to check equality of dictionaries.

I wanted to check only the equality condition on two dictionaries.

Community
  • 1
  • 1
Knight71
  • 2,927
  • 5
  • 37
  • 63
  • I dont get what you are asking. It seems you know what the equal operator does for dictionaries, what else are you asking about? – R Nar Jun 22 '16 at 13:03
  • 3
    I think the interesting question is, _why_ are people iterating key/value pairs manually? Possibly they don't know that == is safe, or maybe they're doing something more exotic than checking regular equality. I'd love to see some examples of these "many places in code". – Kevin Jun 22 '16 at 13:04
  • Where exactly, under what circumstances and in whose code? – jonrsharpe Jun 22 '16 at 13:07
  • I have seen it my organization code base. Also refer here http://stackoverflow.com/questions/4527942/comparing-two-dictionaries-in-python – Knight71 Jun 22 '16 at 13:11
  • I am not an expert in python that is why this question. – Knight71 Jun 22 '16 at 13:11
  • Maybe you should ask your organization why they do it that way. Also, your "refer here" question is about counting the number of matched elements, so it's a different thing. – JJJ Jun 22 '16 at 13:12
  • Well look in the version control logs, find out who wrote it and ask them, we can't tell you why they made that choice. – jonrsharpe Jun 22 '16 at 13:43

1 Answers1

0

To put it simply: yes, the == operator is safe on the built-in Python dictionaries and should always work.

Andrew
  • 388
  • 2
  • 9