I have two dictionaries and I want to compare the values of the corresponding keys. For example, if I have
dict1 = {'a':1, 'b':0, 'c':3}
dict2 = {'a':0, 'b':0, 'c':4}
then this should return False because dict2 can't have a corresponding value larger than that of dict11 (it was okay in dict2 that 'a' had a smaller value than that of dict1, but not okay that 'c' in dict2 had value larger than that of dict1).
Also it is not allowed if dict2 has a value that is not listed in dict1. For instance:
dict1 = {'a':1, 'b':0, 'c':3}
dict2 = {'a':0, 'b':0, 'd':2}
(But it is okay if dict1 has values that dict2 does not). In other words, dict2 has to be a subset of dict1 in regards to both keys and values.
As soon as the code catches one of these violations, I want to immediately stop everything from running and just return False.
This is what I tried:
condition = True #True by default
for letter in dict2:
if dict2[letter] > dict1[letter] or dict1[letter] == None:
condition = False
break
break
But I get a KeyError when I run into a key that's listed in dict1 and not in dict2.
How do I fix this?
try-exceptstatements.