Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Dictionary cmp() method

Python Dictionary cmp() method

Python Dictionary Python Dictionary


Description

The Python Dictionary cmp() function compares two dictionary elements.

Syntax

cmp() method syntax:



cmp(dict1, dict2)

Parameters

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

};

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 : 0

Python Dictionary Python Dictionary