Python Dictionary cmp() Method
Description
The cmp() function of a Python dictionary is used to compare two dictionary elements.
Syntax
cmp() method syntax:
cmp(dict1, dict2)
Parameters
- dict1 -- Dictionary to compare.
- dict2 -- Dictionary to compare.
Return Value
Returns 0 if the elements of the two dictionaries are the same, 1 if dict1 is greater than dict2, and -1 if dict1 is less than dict2.
Example
The following example demonstrates how to use the cmp() function:
Example (Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
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 output of the above example is:
Return Value : -1 Return Value : 1 Return Value : 0Other Extensions
Python Dictionary