Reputation: 467
If there are two dictionaries such that they share multiple keys with one another:
dictionary_1 = {'apple': 0, 'berry': 2}
dictionary_2 = {'apple': 1, 'berry': 1, 'banana': 1}
How can I compare them and determine:
dictionary_2
keys are all in dictionary_1
keys
dictionary_1
are higher than the corresponding values in dictionary_2
?
Upvotes: 0
Views: 52
Reputation: 61063
You can check that dictionary_2.keys()
is a subset of dictionary_1.keys()
with the <=
operator:
print(dictionary_2.keys() <= dictionary_1.keys())
And you can use all
with a generator to check that the values in one dictionary are larger
all(dictionary_1[key] > dictionary_2[key] for key in dictionary_2)
Upvotes: 2