Check if Two Dictionaries are Equal in Python ( Stepwise Solution )

Check if two dictionaries are equal python

Check if two dictionaries are equal python is easily possible with the object equality operator (“==”). If you explicitly want to compare elements wise then use a double-loop iterator. Here you need to iterate one dict and extract the key-value pair of the component. Now you need to look up the value of the corresponding key if it exists and then extract its value of it. Now you need to compare both the values if found equal and if the same case happens with each key then both the dictionaries are equal.

Check if the two dictionaries are equal python( Solution  ) –

Firstly let’s generate two dummy lists. Actually, let’s create three python dictionaries. Here we will make two dictionaries identical and one different from both. This will help in clearing all the concepts.

sample_dict_A={1:'A', 2:'B'}
sample_dict_B={1:'C', 2:'D'}
sample_dict_C={1:'A', 2:'B'}

Solution 1: Using object equality operator (“==”) :

As in the above section, we have created three sample dict in which A and C are identical and A and B are different. Just to validate the same we can use the object equality operator (“==”) simply like below.

Check if two dictionaries are equal python
Check if two dictionaries are equal python

 

Solution 2: Using Iterator and elementwise compare :

As I said this is long root but it will give full confidence in the comparison. Here we will first iterate the sample_dict_A and will extract all the keys in sample_dict_B and match all the value(s). If we found any mismatch then we will immediately break the iterator and it will be a mismatch. Otherwise, if all corresponding keys and values are matching then it is a complete Match.

status=0
for key in sample_dict_A:
    
    value_A= sample_dict_A.get(key)
    value_B= sample_dict_B.get(key)
    if (value_A!=value_B):
      print("Dictianries are not equal")
      status=1
      break
if (status==0) :
  print("Dictionaries are  equal")
compare python dict and check if there are equal using iterator
compare python dict and check if there are equal using iterator

If you want to enhance this approach on a percentage match of dictionaries then you can use the counter update in case of match and divide the length of the dictionary.

Thanks

Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner