Anna Taylor
Anna Taylor

Reputation: 467

Compare keys and values of different dictionaries

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:

Upvotes: 0

Views: 52

Answers (1)

Patrick Haugh
Patrick Haugh

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

Related Questions