How to check if two dictionaries are the same in python ?

Published: November 17, 2020

Tags: Python;

DMCA.com Protection Status

Example of how to check if two dictionaries are the same in python:

Using the python operator ==

Lets consider the dictionary d1

d1 = {'Name':['Bob','Franck','Emma', 'Lucas'], 
      'Age':[12,42,27,8]}

and the dictionary d2

d2 = {'Name':['Bob','Franck','Emma', 'Lucas'], 
      'Age':[12,42,27,81]}

To check if two dictionaries are equals a solution is to use the operator ++

d1 == d2

returns here

True

If we then change d2 a little bit

d2 = {'Name':['Jack','Franck','Emma', 'Lucas'], 
      'Age':[12,42,27,81]}

then

d1 == d2

returns

False

Check if two dictionaries have the same keys

Note to check if two dictionaries have the same keys a solution is to do:

d1.keys() == d2.keys()

returns here

True

References