Comparing Two Dictionaries Key Values and Returning the Value If Match

dictionary, python

Solution

You need to iterate over the keys in crucial and compare each one against the dishes keys. So a directly modified version of your code.

for key in crucial.keys():
    if key in dishes.keys():
        print(dishes[key])

It could be better stated as (no need to specify .keys):

for key in crucial:
    if key in dishes:
        print(dishes[key])

Problem

I'm a beginner to Python, but I've been trying this syntax and I cannot figure it out -- which was been really baffling. ``` crucial = {'eggs': '','ham': '','cheese': ''} dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500} if crucial.keys() in dishes.keys(): print dishes[value] ``` What I want to do is -- if crucial has a key (in this case, `eggs`) in the dishes, it will return `2`. It seems simple enough, but I believe I must be messing some type of syntax somewhere. If someone could guide me a little, that would be greatly appreciated. The real dictionaries I'm comparing with is about 150 keys long, but I'm hoping this code is simple enough.

Original source