comparing dictionaries

Carsten Haese carsten.haese at gmail.com
Wed May 7 10:30:46 EDT 2008


brad wrote:
> I want to compare two dicts that should have identical info just in a 
> different data structure. The first dict's contents look like this. It 
> is authoritative... I know for sure it has the correct key value pairs:
> 
> {'001' : '01'}
> 
> The second dict's contents are like this with a tuple instead of a 
> string for the key:
> 
> {('This is one', '001'): '01'}

It looks like extracting key[1] from each key of the second dictionary 
should yield the keys of the first dictionary. If that is the case, then 
the following should do it:

d1 = {'001':'01', '002':'02'}
d2 = {('This is one', '001'): '01', ('This is two', '002'): '02'}

if d1 == dict( zip( (k[1] for k in d2.keys()), d2.values() ) ):
     print "They're 'equal'."

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net



More information about the Python-list mailing list