minji
minji

Reputation: 61

Checking for exact matches in two dictionaries

I have multiple dictionaries, some consisting of data, and one having values I want to check match in the data dictionaries.

data = {a: '1', b: '2', c: '3', d: '4', e: '5', f: '6', g: '7'}
desiredValues = {b: '2', e: '1', g: '7'} 

For the matching keys in each dictionary, I want to compare the values and see if they match or not. If 1 or more of the values doesn't match, I want to know which ones. Ultimately, I'd like to be able to have some sort of setup where I could compare multiple dictionaries of data to the single desired values dictionary and get an output for each one along the lines of:

b = True
e = False
g = True 

Thank you for taking the time to read my question! Sorry if it's rather simple, I just haven't been able to find a clear way to do this yet.

Upvotes: 1

Views: 381

Answers (5)

David Smolinski
David Smolinski

Reputation: 534

print(data.keys() & desiredValues.keys()) # the intersection of keys sets
for key, _ in data.items():
    if key in desiredValues:
        print(f'{key}: True')
    else:
        print(f'{key}: False')

output:

{'g', 'b', 'e'}
a: False
b: True
c: False
d: False
e: True
f: False
g: True

Upvotes: 1

yatu
yatu

Reputation: 88246

One way could be to check membership by iterating over desiredValues's dict_items and building a set from data for a faster lookup:

set_data = set(data.items()) 

[i in set_data for i in desiredValues.items()]
# [True, False, True]

Upvotes: 1

Mayank Porwal
Mayank Porwal

Reputation: 34076

A basic for loop with if condition would do the trick:

In [144]: for key ,value in desiredValues.items(): 
     ...:     if desiredValues[key] == data[key]: 
     ...:         print('{}= True'.format(key)) 
     ...:     else: 
     ...:         print('{}= False'.format(key)) 
     ...:                                                                                                                                                                                                   
b= True
e= False
g= True

OR

In [143]: for key, value in data.items(): 
     ...:     if key in desiredValues: 
     ...:         if data[key] == desiredValues[key]: 
     ...:             print('{}= True'.format(key)) 
     ...:         else: 
     ...:             print('{}= False'.format(key)) 
     ...:                                                                                                                                                                                                   
b= True
e= False
g= True

Upvotes: 1

Ch3steR
Ch3steR

Reputation: 20669

You can try this.

for k in desiredValues:
    print(f' {k}={data[k]==desiredValues[k]}')

Output:

 b=True
 e=False
 g=True

Upvotes: 1

Victor
Victor

Reputation: 2909

Iterate through desiredValues, then check if the key is in data using data[key] where key is the current key from desiredValues:

data = {'a': '1', 'b': '2', 'c': '3', 'd': '4', 'e': '5', 'f': '6', 'g': '7'}
desiredValues = {'b': '2', 'e': '1', 'g': '7', 'z':'shouldn\'t be in data'} 

for key, value in sorted(desiredValues.items(), key=lambda i: i[0]):
    if key in data:
        print('{}:'.format(key), value == data[key])
    else:
        print('{}:'.format(key), 'not in data')

Output:

b: True
e: False
g: True
z: not in data

Upvotes: 2

Related Questions