Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing Two Dictionaries Key Values and Returning the Value If Match

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.

like image 624
yrekkehs Avatar asked Dec 05 '12 01:12

yrekkehs


People also ask

How do you compare keys in two dictionaries?

Python List cmp() Method. The compare method cmp() is used in Python to compare values and keys of two dictionaries. If method returns 0 if both dictionaries are equal, 1 if dic1 > dict2 and -1 if dict1 < dict2.

How do you check if two dictionaries have the same keys and values?

Check if two nested dictionaries are equal in Python To do this task, we are going to use the == operator and this method will help the user to check whether the two given dictionaries are equal or not.

Can you compare two dictionaries in Python?

You can use the == operator, and it will work. However, when you have specific needs, things become harder. The reason is, Python has no built-in feature allowing us to: compare two dictionaries and check how many pairs are equal.

Can a dictionary have two keys with the same value two values with the same key?

No, each key in a dictionary should be unique. You can't have two keys with the same value. Attempting to use the same key again will just overwrite the previous value stored. If a key needs to store multiple values, then the value associated with the key should be a list or another dictionary.


Video Answer


2 Answers

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]
like image 119
timc Avatar answered Oct 19 '22 23:10

timc


If you're using Python 3, the keys method of dictionaries follows the set interface. That means you can do an intersection of the keys of the two dictionaries using the & operator.

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

Or if you need a list of the values:

result = [dishes[key] for key in crucial.keys() & dishes.keys()]

In Python 2 you could manage the same thing by explicitly creating sets (perhaps from the iterkeys generator), but probably it would be better to simply do a loop over the keys, like several of the other answer suggest.

Here's a variation on the loop that I don't think I've seen anyone else do. The outer loop gets both the keys and values from the dishes dict, so you don't need to separately look up the value by key.

for key, value in dishes.iteritems(): # use items() in Python 3
    if key in crucial:
        print value
like image 23
Blckknght Avatar answered Oct 20 '22 00:10

Blckknght