0

I'm quite the beginner in Python.

I have two dictionaries, where each key is a product and each value is the price to each product.

productData_1 = {'product_1': '18', 'product_2': '15', 'product_3': '10', 'product_4': '9'}
productData_2 = {'product_1': '18', 'product_3': '12', 'product_2': '15'}

What I want to do is compare both dictionaries both by keys and by values, so that I can see the differences between both dicts, e.g. are there different prices, is a product missing in productData_1 or _2?

I know that I have to iterate over both dicts in some way but I can't figure out how to do it.

martineau
  • 119,623
  • 25
  • 170
  • 301
rongon
  • 725
  • 6
  • 20
  • look at this http://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops-in-python – giosans Aug 03 '16 at 09:43
  • 1
    What is your desired output? – Ohad Eytan Aug 03 '16 at 09:43
  • 1
    You'll have to define more clearly what your desired output is. In your post you have multiple questions: do you want as result "product_4" (because it's the only one not in both dicts) or "product_3" (because its value is different in the two dicts), or a combination of both? – ACEG Aug 03 '16 at 09:45
  • 2
    Yes, this question is very likely a duplicate. You should definitely check out the link giosans left in a comment above mine, and decide whether or not to delete this question. There are tons of questions already on StackOverflow about iterating over python dictionaries. – juanpa.arrivillaga Aug 03 '16 at 09:46
  • The top answer in this post includes a full solution http://stackoverflow.com/questions/1165352/calculate-difference-in-keys-contained-in-two-python-dictionaries – Chris_Rands Aug 03 '16 at 09:53
  • You're right Chris, hadn't seen that. – Jacques de Hooge Aug 03 '16 at 09:54

5 Answers5

0

In Python 3.5:

productData_1 = {'product_1': '18', 'product_2': '15', 'product_3': '10', 'product_4': '9'}
productData_2 = {'product_1': '18', 'product_3': '12', 'product_2': '15'}

keys = set (productData_1.keys () + productData_2.keys ())

for key in keys:
    try:
        if productData_1 [key] != productData_2 [key]:
            print ('Mismatch at key {}'.format (key))
    except:
        print ('Entry missing at key {}'.format (key))
Jacques de Hooge
  • 6,750
  • 2
  • 28
  • 45
0
for key in productData_1.keys()|productData_2.keys():
    if key not in productData_1:
        print('{} missing from productData_1!'.format(key))
        continue
    if key not in productData_2:
        print('{} missing from productData_2!'.format(key))
        continue
    print('Difference in prices for key "{}": {}'.format(key,abs(int(productData_1[key])-int(productData_2[key]))))

This code will print if a key is missing from either dict and print the absolute difference between values for each key.

productData_1.keys()|productData_2.keys() will return the union of dictionary keys.

Gábor Fekete
  • 1,343
  • 8
  • 16
0
keys_unique_to_1 = [key for key in productData_1 if key not in productData_2]
keys_unique_to_2 = [key for key in productData_2 if key not in productData_1]
common_keys = [key for key in productData_1 if key in productData_2]

diff = {key: int(productData_1[key]) - int(productData_2[key]) for key in common_keys}

print 'Keys unique to 1: ', keys_unique_to_1
# Keys unique to 1:  ['product_4']

print 'Keys unique to 2: ', keys_unique_to_2
# Keys unique to 2:  []

print 'Difference: ', diff
# Difference:  {'product_1': 0, 'product_3': -2, 'product_2': 0}
Julien Spronck
  • 15,069
  • 4
  • 47
  • 55
0

There's a good page here on this issue. You can loop through both your dictionaries and compare their values. The example hereunder shows how you can print the keys for which your values are equal.

=========================================================================

productData_1 = {'product_1': '18', 'product_2': '15', 'product_3': '10', 'product_4': '9'} productData_2 = {'product_1': '18', 'product_3': '12', 'product_2': '15'}

For Python 2.x-

for key_1, value_1 in productData_1.iteritems():
    for key_2, value_2 in productData_2.iteritems():
    if value_1 == value_2:
         print("key_1" + str(key_1))
         print("key_2" + str(key_2))

For Python 3.x-

for key_1, value_1 in productData_1.items():
     for key_2, value_2 in productData_2.items():
     if value_1 == value_2:
         print("key_1" + str(key_1))
         print("key_2" + str(key_2))
Community
  • 1
  • 1
datahero
  • 101
  • 5
0
productData_1 = {'product_1': '18', 'product_2': '15', 'product_3': '10', 'product_4': '9'}
productData_2 = {'product_1': '18', 'product_3': '12', 'product_2': '15'}

matched_keys = []
unmatched_keys = []
matched_value = []
unmatched_value = []

for ind,value in productData_1.items():
    if ind in productData_2.keys():
        matched_keys.append( ind )
        if value in productData_2.values():
            matched_value.append( value )
        else:
            unmatched_value.append( value )
    else:
        unmatched_keys.append( ind )
        if value in productData_2.values():
            matched_value.append( value )
        else:
            unmatched_value.append( value )



print matched_keys
print unmatched_keys

print matched_value
print unmatched_value
Athar
  • 963
  • 10
  • 23