Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two dictionaries in Python by identifying sets with same key but different values

I'm trying to compare two dictionaries by comparing the keys, if two keys in the two seperate dictionaries are the same the program should check if the values are also the same, if they are not the same the program should identify that.

This is the code I have written:

def compare(firstdict,seconddict):
    shared_items = set(firstdict()) & set(seconddict())
    length = len(shared_items)
    if length > 0:
        return shared_items
    if length < 1:
        return None
print(compare(firstdict,seconddict))

('firstdict' and 'seconddict' are two dictionaries which have been made in previous functions).

When the code is run it prints out all the keys that are the same without their values, even if their values are different.

For example if:

firstdict = {'cat' : 'animal', 'blue' : 'colour', 'sun' : 'star'}

seconddict = {'cat' : 'pet', 'blue' : 'colour', 'earth' : 'star'}   

it would print out:

'cat', 'blue'

whereas I'm tring to get it to print out:

'cat pet (animal)'

in that exact format.

Any advice on how to edit my code to do this is appreciated :)

like image 253
Sara Avatar asked Mar 17 '23 17:03

Sara


2 Answers

You can use set intersection on the dictionaries keys(). Then loop over those and check if the values corresponding to those keys are identical. If not, you can print them out with format.

def compare(first, second):
    sharedKeys = set(first.keys()).intersection(second.keys())
    for key in sharedKeys:
        if first[key] != second[key]:
            print('Key: {}, Value 1: {}, Value 2: {}'.format(key, first[key], second[key]))

>>> compare(firstdict, seconddict)
Key: cat, Value 1: animal, Value 2: pet

And for another example

>>> firstdict = {'cat' : 'animal', 'blue' : 'colour', 'sun' : 'star', 'name': 'bob', 'shape': 'circle'}
>>> seconddict = {'cat' : 'pet', 'blue' : 'colour', 'earth' : 'star', 'name': 'steve', 'shape': 'square'}

>>> compare(firstdict, seconddict)
Key: shape, Value 1: circle, Value 2: square
Key: cat, Value 1: animal, Value 2: pet
Key: name, Value 1: bob, Value 2: steve
like image 87
Cory Kramer Avatar answered Apr 30 '23 07:04

Cory Kramer


If you values are hashable also you can use items to get the common key/value pairings:

firstdict = {'cat' : 'animal', 'blue' : 'colour', 'sun' : 'star'}

seconddict = {'cat' : 'pet', 'blue' : 'colour', 'earth' : 'star'}

common = set(firstdict.iteritems()).intersection(seconddict.iteritems())

for k,v in common:
    print("Key: {}, Value: {}".format(k,v))
Key: blue, Value: colour

To check if both dicts are the same, check the len of each:

print(len(common)) == len(firstdict)

To find common keys with different value:

for k,v in firstdict.iteritems():
    if k in seconddict and seconddict[k] != v:
        print("Key: {}, Value: {}".format(k, seconddict[k]))
Key: cat, Value: pet
like image 40
Padraic Cunningham Avatar answered Apr 30 '23 09:04

Padraic Cunningham