How to compare two dictionaries in Python
In this Python tutorial, we will understand how to check if two dictionaries are equal in Python. Here we will see how to compare dictionaries with one another and find if both are the same.
There are mainly 4 methods to check if two dictionaries are equal in Python. All 4 methods are listed and discussed below.
- Using == operator
- Using for loop
- Using list comprehension
- Using deepdiff
Table of Contents
How to check if two dictionaries are equal in Python
Here we will discuss all 4 main methods to check if two dictionaries are equal in Python. And we start with the first basic method of using the == operator in Python.
Method 1: Check if two dictionaries are equal in Python using the == operator
One of the basic ways to compare two strings in Python is by using the == comparison operator in Python.
The == is a comparison operator in Python that we can use to check if two objects are equal or not in Python. Moreover, we can use this operator with Python Dictionary as well.
So, let us see an example of using the == operator with the dictionary.
In the above example, we have defined two dictionaries with the same data. And then we are comparing both the dictionaries using == operator and if statement.
And as both dictionaries are the same, we will get the following result.
However, let us change the value of any key from any of the dictionaries and compare again.
Here we modified the value of age from the user_info2 dictionary. And this time, as the dictionaries are not equal, we will get the following result.
Method 2: Check if two dictionaries are equal in Python using for loop
- In this method, we will use the dict.items() method to fetch the key and value of a dictionary separately.
- Next, we will use the for loop to iterate over each key-value pair and use the if-else statement to compare if key-value pairs are the same in both dictionaries.
Here is an example of this approach in Python.
In the above example, first, we defined two dictionaries, then we defined a function that will accept 2 dictionaries.
This function will use for loop with dict.items() method to iterate over each key-value pair. And within the loop, it uses the if statement to compare both keys and values.
In the last, the function returns a statement based on the dictionary comparison.
Now, let us change the value from one dictionary and compare both dictionaries again in Python.
Here is the result once the change value from the 2nd dictionary.
Method 3: Check if two dictionaries are equal in Python using list comprehension
- In this method, we will use the list comprehension method to fetch each key and value from both dictionaries.
- After this, we are comparing values to check if both dictionaries are the same. This will result in forming a list of boolean values.
- Now, to check if all the values in the list are TRUE, we will use the all() function on the boolean list. And based on this, we will conclude if both dictionaries are the same.
Here is an example of this approach in Python.
In the above code, we are comparing two same dictionaries in Python. So, after execution, we will get the following result.
Let us test our function by changing certain values and comparing both dictionaries again.
Now, as both dictionaries are not the same, we will get the following result in Python.
Method 4: Check if two dictionaries are equal in Python using deepdiff
The deepdiff is a powerful library that helps to compare two Python objects and check if both objects are the same or not. This library also allows us to find the difference between both objects in Python.
However, to use this library first, we need to install this library in Python. For this, we will use the following command first.
Once we have installed this library successfully, we can follow the given example.
In this example, we have defined a function named compare_dict() which will accept 2 dictionary values. And this function will use the DeepDiff class to compare both dictionaries and return a result based on it.
So, once we call the function and pass two same dictionaries to it, we will get the following result.

Additionally, we can also test this function by passing two different dictionaries to it.
Here we modified the value for one dictionary and compared them using the compare_dict() function.
The result for the above execution is given below.
You may also like to read the following Python tutorials.
Conclusion
So, in this Python tutorial, we understood how to check if two dictionaries are equal in Python or not. And for this implementation in Python, we covered 4 different methods. These 4 methods are listed below.
- Using == operator
- Using for loop
- Using list comprehension
- Using deepdiff

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.
Comparing two dictionaries and checking how many (key, value) pairs are equal
I have two dictionaries, but for simplification, I will take these two:
Now, I want to compare whether each key, value pair in x has the same corresponding value in y . So I wrote this:
And it works since a tuple is returned and then compared for equality.
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.
![]()
28 Answers 28
If you want to know how many values match in both the dictionaries, you should have said that 🙂
Maybe something like this:
![]()
![]()
What you want to do is simply x==y
What you do is not a good idea, because the items in a dictionary are not supposed to have any order. You might be comparing [(‘a’,1),(‘b’,1)] with [(‘b’,1), (‘a’,1)] (same dictionaries, different order).
For example, see this:
The difference is only one item, but your algorithm will see that all items are different
dic1 == dic2
The following examples all return a dictionary equal to <"one": 1, "two": 2, "three": 3>:
Providing keyword arguments as in the first example only works for keys that are valid Python identifiers. Otherwise, any valid keys can be used.
Valid for python2 and python3 .
![]()
Since it seems nobody mentioned deepdiff , I will add it here for completeness. I find it very convenient for getting diff of (nested) objects in general:
Installation
Sample code
Output
Note about pretty-printing the result for inspection: The above code works if both dicts have the same attribute keys (with possibly different attribute values as in the example). However, if an «extra» attribute is present is one of the dicts, json.dumps() fails with
Solution: use diff.to_json() and json.loads() / json.dumps() to pretty-print:
Alternative: use pprint , results in a different formatting:
![]()
![]()
I’m new to python but I ended up doing something similar to @mouad
The XOR operator ( ^ ) should eliminate all elements of the dict when they are the same in both dicts.
@mouad ‘s answer is nice if you assume that both dictionaries contain simple values only. However, if you have dictionaries that contain dictionaries you’ll get an exception as dictionaries are not hashable.
Off the top of my head, something like this might work:
![]()
The function is fine IMO, clear and intuitive. But just to give you (another) answer, here is my go:
Can be useful for you or for anyone else..
I have created a recursive version of the one above.. Have not seen that in the other answers
The easiest way (and one of the more robust at that) to do a deep comparison of two dictionaries is to serialize them in JSON format, sorting the keys, and compare the string results:
Yet another possibility, up to the last note of the OP, is to compare the hashes ( SHA or MD ) of the dicts dumped as JSON. The way hashes are constructed guarantee that if they are equal, the source strings are equal as well. This is very fast and mathematically sound.
To test if two dicts are equal in keys and values:
If you want to return the values which differ, write it differently:
You would have to call it twice i.e
A simple compare with == should be enough nowadays (python 3.8). Even when you compare the same dicts in a different order (last example). The best thing is, you don’t need a third-party package to accomplish this.
![]()
I am using this solution that works perfectly for me in Python 3
It compares dict, list and any other types that implements the «==» operator by themselves. If you need to compare something else different, you need to add a new branch in the «if tree».
Hope that helps.
In PyUnit there’s a method which compares dictionaries beautifully. I tested it using the following two dictionaries, and it does exactly what you’re looking for.
I’m not recommending importing unittest into your production code. My thought is the source in PyUnit could be re-tooled to run in production. It uses pprint which «pretty prints» the dictionaries. Seems pretty easy to adapt this code to be «production ready».
![]()
Being late in my response is better than never!
Compare Not_Equal is more efficient than comparing Equal. As such two dicts are not equal if any key values in one dict is not found in the other dict. The code below takes into consideration that you maybe comparing default dict and thus uses get instead of getitem [].
Using a kind of random value as default in the get call equal to the key being retrieved — just in case the dicts has a None as value in one dict and that key does not exist in the other. Also the get != condition is checked before the not in condition for efficiency because you are doing the check on the keys and values from both sides at the same time.
![]()
Why not just iterate through one dictionary and check the other in the process (assuming both dictionaries have the same keys)?
This way you can subtract dictView2 from dictView1 and it will return a set of key/value pairs that are different in dictView2:
You can intersect, union, difference (shown above), symmetric difference these dictionary view objects.
Better? Faster? — not sure, but part of the standard library — which makes it a big plus for portability