Python Dictionary cmp() method
Description
The Python Dictionary cmp() function compares two dictionary elements.
Syntax
cmp() method syntax:
cmp(dict1, dict2)
Parameters
- dict1 -- the dictionary to compare.
- dict2 -- the dictionary to compare.
Return Value
Returns 0 if the two dictionaries have the same elements, 1 if dict1 is greater than dict2, and -1 if dict1 is less than dict2.
Example
The following example shows 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
};
"
Return Value : %d
"
%
cmp
(
dict1
,
dict2
)
"
Return Value : %d
"
%
cmp
(
dict2
,
dict3
)
"
Return Value : %d
"
%
cmp
(
dict1
,
dict4
)
The output of the above example is:
Return Value : -1
Return Value : 1
Return Value : 0
Python Dictionary