How to compare nested dictionary values from the same dict key using Python

How to compare nested dictionary values from the same dict key using Python



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


Content_05


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)





It's not clear what you are asking or doing here. What is the goal, can you describe what you are trying to achieve?
– Martijn Pieters
Sep 4 '18 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.
– Martijn Pieters
Sep 4 '18 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.
– Auto-learner
Sep 4 '18 at 13:48





Please edit your question to make that clear. What about missing filenames?
– Martijn Pieters
Sep 4 '18 at 13:49





Are the top-level keys always the same? If not, are they always going to be present in both locations?
– Martijn Pieters
Sep 4 '18 at 13:50




1 Answer
1



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:


dict.get()


dict.get()


None


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.


key_05_1





Thanks @Pieters for the clear answer and explanation.
– Auto-learner
Sep 4 '18 at 14:00



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?