In Python, you can compare two dictionaries using the ==
operator. This operator compares the key-value pairs of both dictionaries, returning True
if they are equal and False
if they are not.
Here's an example program that compares two dictionaries in Python:
dict1 = {'a': 1, 'b': 2, 'c': 3} dict2 = {'a': 1, 'b': 2, 'c': 3} if dict1 == dict2: print("The dictionaries are equal") else: print("The dictionaries are not equal")
In this example, we create two dictionaries dict1
and dict2
with the same key-value pairs. We then use the ==
operator to compare the two dictionaries. Since the dictionaries are equal, the program will output "The dictionaries are equal".
If the dictionaries had different key-value pairs, the ==
operator would return False
and the program would output "The dictionaries are not equal".
Note that the order of the key-value pairs does not matter when comparing dictionaries. As long as the key-value pairs are the same, the dictionaries are considered equal.