Equal to (==) operator compares two dictionaries and returns True if both are dictionaries are equal, False otherwise.
# Python program to compare two dictionaries
# using == operator
record1 = {'id': 101, 'name': 'Shivang Yadav', 'Age': 21}
record2 = {'id': 101, 'name': 'Shivang Yadav', 'Age': 21}
record3 = {'id': 102, 'name': 'Radib Kar', 'Age': 23}
if record1 == record2:
print("record1 is equal to record2")
else:
print("record1 is not equal to record2")
if record2 == record3:
print("record2 is equal to record3")
else:
print("record2 is not equal to record3")
Output:
record1 is equal to record2
record2 is not equal to record3
2) Compare two dictionaries using DeepDiff() method
The DeepDiff() method is from "deepdiff" module and it compares the difference between two dictionaries (other collections also), and returns the changed values.
1) Compare two dictionaries using == operator
Equal to (==) operator compares two dictionaries and returns True if both are dictionaries are equal, False otherwise.
Output:
2) Compare two dictionaries using DeepDiff() method
The DeepDiff() method is from "deepdiff" module and it compares the difference between two dictionaries (other collections also), and returns the changed values.
Output:
Note: You need to install the module "deepdiff" first.
need an explanation for this answer? contact us directly to get an explanation for this answer