Comparing Dictionaries

Ben Finney bignose+hates-spam at benfinney.id.au
Fri Jul 27 04:55:39 CEST 2007


Kenneth Love <klove at tax.ok.gov> writes:

> In other words, I consider these two dictionaries to be equivalent:
> 
>     { 'dog' : 'bone', 'cat' : 'fever', 'mouse' : 'mickey' }
>     { 'mouse' : 'mickey', 'dog' : 'bone', 'cat' : 'fever' }
> 
> while these two are not:
> 
>     { 'dog' : 'bone', 'cat' : 'fever', 'mouse' : 'mickey' }
>     { 'dog' : 'bone', 'cat' : 'fever', 'mouse' : 'michael' }
> 
> Any suggestions on how to compare these via some assert statement?

Dictionaries already know how to compare themselves to each other.

    >>> {'dog': 'bone', 'cat': 'fever', 'mouse': 'mickey'} == {'mouse': 'mickey', 'dog': 'bone', 'cat': 'fever'}
    True
    >>> {'dog': 'bone', 'cat': 'fever', 'mouse': 'mickey'} == {'mouse': 'michael', 'dog': 'bone', 'cat': 'fever'}
    False

-- 
 \     "Many are stubborn in pursuit of the path they have chosen, few |
  `\                  in pursuit of the goal."  -- Friedrich Nietzsche |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list