Search code snippets, questions, articles...

Check if two dictionaries are equal in Python

Dictionaries are a handy data type in Python, that allows you to work with key-value pairs, which can be retrieved either by their key or their value. In this post, we will learn how to check if two dictionaries are equal or not using multiple methods.
dict1 = {"a": 1, "b": 2, "c": 4}
dict2 = {"a": 1, "b": 2, "c": 3}

if dict1 == dict2:
    print("The dictionaries are equal")
else:
    print("The dictionaries are not equal")
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

Output

The dictionaries are not equal

Use == operator to check if the dictionaries are equal

The simplest technique to check if two or multiple dictionaries are equal is by using the == operator in Python. You can create the dictionaries with any of the methods defined in Python and then compare them using the == operator. It will return True the dictionaries are equals and False if not.

Syntax

dict1 == dict2 == dict3

Code example

dict1 = dict(a=1, b=2, c=3)
dict2 = dict(zip(['a', 'b', 'c'], [1, 2, 3]))
dict3 = dict([('b', 2), ('a', 1), ('c', 3)])
dict4 = dict({'b': 2, 'a': 1, 'c': 3})

# compare all dictionaries
if dict1 == dict2 == dict3 == dict4:
  print('The dictionaries are equal')

# compare two dictionaries
if dict2 == dict4:
  print('The dictionaries are equal')

Output

The dictionaries are equal
The dictionaries are equal

Explanation of the above code example

  1. Created four dictionaries(dict1, dict2, dict3, dict4) using different methods.
  2. Using dict1 == dict2 == dict3 == dict4 code syntax we are comparing all the dictionaries. This will return True if all the dictionaries are equal.
  3. Using dict2 == dict4 code, we are comparing two dictionaries.
  4. Print message - "The dictionaries are equal" if the dictionaries are equal.

Compare dictionaries using the deepdiff python module

You can install the Python module deepdiff in your project and check the differences between the two dictionaries. We can install the deepdiff module using the below command in your project.

pip install deepdiff

We will be using the DeepDiff() function from the deepdiff module to compare the dictionaries. It will return the dictionary that will contain the information of the difference between the dictionaries.

Syntax

DeepDiff(dict1, dict2)
from deepdiff import DeepDiff

dict1 = {'a': 1, 'b': 2, 'c': 4}
dict2 = {'a': 1, 'c': 3, 'd': 5}

result = DeepDiff(dict1, dict2)

print(result)

Output

{'dictionary_item_added': [root['d']], 'dictionary_item_removed': [root['b']], 'values_changed': {"root['c']": {'new_value': 3, 'old_value': 4}}}

Get common key-value pairs from two dictionaries

We can get the common key-value pairs from two dictionaries and check if those are equal or not. We will be using Python For loop to the common key-value pairs.

  1. Create two dictionaries that contain some properties.
  2. Create a blank dictionary that will contain the common key-value pairs.
  3. In each iteration of For Loop check if the key from the second dictionary exists in the first dictionary. Also, check if the value from both dictionaries is the same for the same key. If this condition is found true, add that key-value pair to the common dictionary variable.
dict1 = {"a": 1, "b": 2, "c": 3}
dict2 = {"a": 3, "b": 2, "c": 3, "d": 4}

common = {}

for key in dict1:
  if ((key in dict2) and (dict1[key] == dict2[key])):
    common[key] = dict1[key]

# print common key-value pairs
print(common)

Output

{'b': 2, 'c': 3}
Was this helpful?
0 Comments