[Tutor] Test to check if values of dictionary are all equal (which happen to be dictionaries)

Jignesh Sutar jignesh.sutar at gmail.com
Sun Nov 9 16:22:18 CET 2014


I needed to test if the values of all entries in a dictionary were equal
but since the values themselves were dictionaries I couldn't simply take a
set of the values and test if this equated to one. So I ended up taking all
combination of the keys and testing pairs of sub dictionaries. I just want
to check that there isn't a more direct way of doing this that testing all
combinations?


import itertools

dictmain={"K1": {1:"SD_V1",2:"SD_V2"},
          "K2": {1:"SD_V1",2:"SD_V2"},
          "K3": {1:"SD_V1",2:"SD_V2"}}

for compare in list(itertools.combinations(dictmain,2)):
    print "Comparing dictionaries:", compare

    if dictmain[compare[0]]==dictmain[compare[1]]:
        print "comb dict are equal"
    else:
        print "comb dict are NOT equal"
        break


Many thanks in advance,
Jignesh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20141109/508c1c1e/attachment.html>


More information about the Tutor mailing list