Check if two dictionaries are equal in Python

In this Python tutorial, we will discuss the How to check if two dictionaries are equal in Python. Moreover, we will also cover the following topics:

  • Check if two dictionaries are equal in Python
  • Check if two nested dictionaries are equal in Python
  • Check if two lists of dictionaries are equal in Python

Check if two dictionaries are equal in Python

  • In this section, we will learn how to check if two dictionaries are equal in Python.
  • To perform this particular task, we are going to use the == operator and this operand is available in the Python package. In Python, this function will always return the boolean value that indicates either the value is False or True. It will check the condition if the given values are equal then it will return ‘True’ otherwise it will return ‘False’.
  • In this program, we have to check if two dictionaries are equal in Python. To do this task, first, we will create dictionaries named ‘my_dictionary’ , ‘my_dictionary2’ and assign the key-value pair elements.
  • Next, we will apply the if-else condition if the given dictionaries are equal then it displays “Dictionaries are equal”. If the given dictionaries are not equal then it will display “Dictionaries are not equal”.

Example:

my_dictionary = {'U.S.A': 167, 'United Kingdom': 156, 'China': 456}
my_dictionary2 = {'U.S.A': 167, 'United Kingdom': 156, 'China': 456}
  
if my_dictionary == my_dictionary2:
    print ("Dictionaries are equal")
else:
    print ("Dictionaries are not equal")

Here is the Screenshot of the following given code.

Check if two dictionaries are equal in Python
Check if two dictionaries are equal in Python

As you can see in the Screenshot, the output displays “Dictionaries are equal”.

Now, there can be one more case when the given dictionaries are not equal. Let’s take an example and check how to use the == operator for checking the two equal dictionaries in Python.

Source Code:

my_dictionary = {'U.S.A': 167, 'United Kingdom': 156, 'China': 456}
my_dictionary2 = {'U.S.A': 167, 'United Kingdom': 156,}
  
if my_dictionary == my_dictionary2:
    print ("Dictionaries are equal")
else:
    print ("Dictionaries are not equal 'False'")
  • In the above code, we have created the same dictionaries but in this case, the second dictionary ‘my_dictionary2’ has only 2 elements in the form of key-value pair.
  • Once you will execute this code, the output displays “Dictionaries are not equal”.
  • The reason behind this is the first dictionary ‘my_dictionary’ contains 3 elements while the second dictionary contains 2 elements.

Here is the execution of the following given code.

Check if two dictionaries are equal Python
Check if two dictionaries are equal in Python

Note: In the Python dictionary method cmp() is depletion for Python 3.x as per the official documentation.

Check if two nested dictionaries are equal in Python

  • In this example, we will learn how to check if two nested dictionaries are equal in Python.
  • To do this task, we are going to use the == operator and this method will help the user to check whether the two given dictionaries are equal or not.
  • In Python dictionary, data is stored in the form of a key-value pair and nested dictionary represents the dictionary inside the dictionary and stores information in a structured way.
  • In this example, first, we will create a nested dictionary and assign the country name along with their random values. Next, we will use the if-else condition and check if the nested dictionaries are equal or not.

Example:

Let’s take an example and check how to check if two nested dictionaries are equal in Python.

Source Code:

my_new_dict = {'U.S.A': {'Japan': {'Germany': 10}, 'Australia': {'China': 20}}}
my_new_dict2 = {'U.S.A': {'Japan': {'Germany': 10}, 'Australia': {'China': 20}}}

if my_new_dict == my_new_dict2:
    print ("Dictionaries are equal 'True")
else:
    print ("Dictionaries are not equal 'False'")

Here is the implementation of the following given code.

Check if two nested dictionaries are equal Python
Check if two nested dictionaries are equal Python

As you can see in the Screenshot, the output displays the dictionaries are equal.

How to check if two nested dictionaries are equal Python another approach

  • In this Program, we can easily use the Deepdiff library to check if two nested dictionaries are equal or not. First, you have installed this package in your machine by using the pip install DeepDiff command. This method will check the difference between dictionaries.
  • Suppose you have two dictionaries that contain elements in the form of key-value pairs. Now, in the first dictionary, you have inserted the value 10, and in the other one, you have inserted 11.
  • Once you will use this function ‘deepDiff’ then compare two dictionaries and check how many pairs are equal. If it is not equal then it displays ‘new_value’ and ‘old_value’.

Source Code:

from deepdiff import DeepDiff

my_new_dict = {
    'United kingdom': {
        'U.S.A': {'France': 4560}, 
    'China': {'Japan': 10}
    }}
my_new_dict2 = {
    'United kingdom': {
        'U.S.A': {'France': 4560}, 
    'China': {'Japan': 10}
    }}

if DeepDiff(my_new_dict, my_new_dict2):
    print ("Dictionaries are not equal")
else:
    print ("Dictionaries are equal")

In the above code, we have just used the if-else condition and checked if the dictionaries are equal or not. If the dictionaries are equal then, it will display “Dictionaries are equal” otherwise it will return “Dictionaries are not equal”.

You can refer to the below Screenshot.

How to check if two nested dictionaries are equal Python
How to check if two nested dictionaries are equal Python

Read: Python dictionary of lists

Check if two lists of dictionaries are equal Python

  • In this section, we will learn how to check if two lists of dictionaries are equal Python.
  • In this example, we will create a dictionary and assign the elements in the form of key-value pairs. Now, Key will be considered as country name and values will be considered as ‘values’ in the list.
  • Next, we will use the == operator and it compares two given dictionaries and check whether the dictionary elements are equal or not.

Example:

Let’s have a look at the example and understand how to use the == operator in Python.

Source Code:

new_dict = {
    'Country_name': 'U.S.A',
    'values': [56,78,97]
   }

new_dict2 = {
    'Country_name': 'U.S.A',
    'values': [56,78,97]
   }
if new_dict == new_dict2:
    print ("Dictionaries are equal 'True")
else:
    print ("Dictionaries are not equal 'False'")

Here is the Screenshot of the following given code.

Check if two lists of dictionaries are equal Python
Check if two lists of dictionaries are equal Python

You may also like to read the following Python tutorials.

In this Python tutorial, we have learned how to check if two dictionaries are equal in Python.

  • Check if two dictionaries are equal in Python
  • Check if two nested dictionaries are equal in Python
  • Check if two lists of dictionaries are equal in Python