1

I have two dictionaries Content_11 and Content_05, inside the dictionary I have checksum for each file which i need to compare , if checksum is matching print something like success else failure for that filename . Following is my data structure and my code snippet is below.

Content_11 = {
    "controls": {
        "windows-library-1234.zip": "A123455adfasfasdfasdf",  # SHA 256 checksum
        "unix-library-1234.zip": "a2343dfasdfasdfasdfasdfasdfasdf"
    },
    "policies": {
        "oracle-1234.zip": "A123455adfasfasdfasdfad",
        "rhel7-1234.zip": "sdaf23234234234asdf",
    }
}

Content_05 = {
    "controls": {
        "windows-library-1234.zip": "A123455adfasfasdfasdf",
        "unix-library-1234.zip": "a2343dfasdfasdfasdfasdfasdfasdf"
    },
    "policies": {
        "oracle-1234.zip": "A123455adfasfasdfasdfad",
        "rhel7-1234.zip": "sdaf23234234234asdf",
    }
}

I went through some of the questions from stackoverflow and i didnt find the one relevant to me. Any suggestions or improvements are appreciated.

for key_05, value_05 in Content_05.items():  # iterating inside content_05 dict
    for key_05_1, value_05_1 in value_05.items():  # iterating inside the content_05 value for getting nested dict
        for key_011, value_011 in Content_11.items():  # iterating insde content_11 dict for comparison
            for key_11_1, value_11_1 in value_011.items():
                if key_05 == key_011:
                    if value_05_1 == value_11_1:
                        print "Key {} and its value is {} is matching with {} and hence Success".format(key_05_1,
                                                                                                        value_05_1,
                                                                                                        value_11_1)
                    else:
                        print "Key {} and its value is {} is not matching with {} and hence FAILURE".format(key_05_1,
                                                                                                        value_05_1,
                                                                                                        value_11_1)
6
  • It's not clear what you are asking or doing here. What is the goal, can you describe what you are trying to achieve? Commented Sep 4, 2018 at 13:47
  • Note that you don't need to loop over both dictionaries. You already have the key for the first dictionary, just look up the value for the same key in the other. Commented Sep 4, 2018 at 13:47
  • I have two dictionaries Content_11 and Content_05, inside the dictionary I have checksum for each file which i need to compare , if checksum is matching print something like success else failure for that filename. Commented Sep 4, 2018 at 13:48
  • Please edit your question to make that clear. What about missing filenames? Commented Sep 4, 2018 at 13:49
  • Are the top-level keys always the same? If not, are they always going to be present in both locations? Commented Sep 4, 2018 at 13:50

1 Answer 1

4

You are doing way too much work; there is no need to loop over both dictionaries, as you can just test if keys from one of the dictionaries are available in the other.

You could use dict.get() to return a default value and simplify testing further; by default dict.get() returns None for a missing value, just use that in the comparison:

for type_, checksums in Content_05.items():
    # type_ is assumed to be present in the other dictionary
    other_checksums = Content_11[type_]

    for filename, hash in checksums.items():
        other_hash = other_checksums.get(filename)
        if hash != other_hash:
            print("Failure: Checksum mismatch for {}, expected {}, got {}".format(
                filename, hash, other_hash))
        else:
            print("Success: {} checksum matched".format(filename))

I tried to use more legible variable names too; filename, checksums and hashes is a lot more comprehensible than key_05_1, etc.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Pieters for the clear answer and explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.