Latest web development tutorials

Python dictionary (Dictionary) cmp () method


Python dictionary cmp () function is used to compare two elements in the dictionary.


cmp () method syntax:

cmp(dict1, dict2)


  • dict1 - Comparison of dictionaries.
  • dict2 - Comparison of dictionaries.

return value

If the two elements of the same dictionary returns 0 if the dictionary is greater than dict1 dictionary dict2 returns 1 if the dictionary dict1 dictionary dict2 less than -1.


The following example shows cmp () function to use:


dict1 = {'Name': 'Zara', 'Age': 7};
dict2 = {'Name': 'Mahnaz', 'Age': 27};
dict3 = {'Name': 'Abid', 'Age': 27};
dict4 = {'Name': 'Zara', 'Age': 7};
print "Return Value : %d" %  cmp (dict1, dict2)
print "Return Value : %d" %  cmp (dict2, dict3)
print "Return Value : %d" %  cmp (dict1, dict4)

The above example output is:

Return Value : -1
Return Value : 1
Return Value : 0