Skip to content
Advertisement

Comparing two dictionaries and checking how many (key, value) pairs are equal

I have two dictionaries, but for simplification, I will take these two:

JavaScript

Now, I want to compare whether each key, value pair in x has the same corresponding value in y. So I wrote this:

JavaScript

And it works since a tuple is returned and then compared for equality.

My questions:

Is this correct? Is there a better way to do this? Better not in speed, I am talking about code elegance.

UPDATE: I forgot to mention that I have to check how many key, value pairs are equal.

Advertisement

Answer

If you want to know how many values match in both the dictionaries, you should have said that :)

Maybe something like this:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement