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

user225312 :

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

>>> x = dict(a=1, b=2)
>>> y = dict(a=2, b=2)

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

>>> for x_values, y_values in zip(x.iteritems(), y.iteritems()):
        if x_values == y_values:
            print 'Ok', x_values, y_values
        else:
            print 'Not', x_values, y_values

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.

mouad :

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

Maybe something like this:

shared_items = {k: x[k] for k in x if k in y and x[k] == y[k]}
print len(shared_items)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I merge two dictionaries with multiple key value pairs

Find matching key-value pairs of two dictionaries

Combining Key-Value Pairs from two Dictionaries

Merging two or more dictionaries when they have the same key value pairs

How to find the difference between two lists of dictionaries checking the key-value pair

How to create nested dictionary using two dictionaries in which value of first dictionary is equal to the key of second dictionary

How to find common key value pairs from within nested dictionaries

How to convert a string with key value pairs into a list of dictionaries?

How to add two equal sized lists into one list with key pairs

how to create a list of values from one key-value pair based on the value of another k-v pair from a list of dictionaries with two key-value pairs

Compare the keys of two dictionaries and create a dictionary with key, value pairs that does not match the key in the other dictionary - Python

Compare dictionaries and delete key:value pairs

Flatten list of dictionaries with multiple key, value pairs

Creating a list of dictionaries with numerous key value pairs

Returning key-value pairs in a list of dictionaries

how many key value pairs an object can handle?

Python sum values of list of dictionaries if two other key value pairs match

Comparing three (or more) dictionaries and finding a match if at least two are equal

Get dictionaries in a list where the key:value pairs are in another list of dictionaries

How to compare two dictionaries by value, even if the key or value are reference types?

Checking if two consecutive pairs of coordinates in an arraylist are equal in java

Python Dictionaries: Grouping Key, Value pairs based on a common key, value

Find how many equal pairs there is in input

How to get a subset of comparing two vectors of pairs?

How to retrieve common key value pairs from two hashmaps

How map Key/Value pairs between two separate RDDs?

How to query a struct in BigQuery where two key: value pairs matter

How to compare two lists of dicts for multiple key, value pairs?

How to merge the key-value pairs of two json arrays? - Javascript