0

I want to know how do I compare 2 dictionary in python to enable and create into 1 dictionary that if both dictionary happen to have the same keys then it will be 1 keys in the new dictionary and the values of the the same keys will be add together and will be the new values for the 1 keys for eg.

def view_cart():
    if shopping_cart.keys() in shopping_cart_2.keys:
        new_shop = shopping_cart.values + shopping_cart_2.values 
        print(new_shop)

shopping_cart = {'milk': 1, 'eggs': 1, 'bread': 2}
shopping_cart_2 = {'milk': 1 ,'coke': 3, 'oyster sauces': 1}
Samwise
  • 68,105
  • 3
  • 30
  • 44
elsonn
  • 11
  • 1
  • Please reindent the code in the question and format it as code. – NelsonGon Aug 06 '22 at 16:44
  • Does this answer your question? [Is there any pythonic way to combine two dicts (adding values for keys that appear in both)?](https://stackoverflow.com/questions/11011756/is-there-any-pythonic-way-to-combine-two-dicts-adding-values-for-keys-that-appe) – fsimonjetz Aug 06 '22 at 16:47

2 Answers2

1

Combine the two sets of keys by turning them into sets and taking the union; then iterate over that union set and add the count from each dictionary.

>>> shopping_cart = {'milk': 1, 'eggs': 1, 'bread': 2}
>>> shopping_cart_2 = {'milk': 1 ,'coke': 3, 'oyster sauces': 1}
>>> {key: shopping_cart.get(key, 0) + shopping_cart_2.get(key, 0) for key in set(shopping_cart) | set(shopping_cart_2)}
{'coke': 3, 'milk': 2, 'eggs': 1, 'bread': 2, 'oyster sauces': 1}
Samwise
  • 68,105
  • 3
  • 30
  • 44
  • bro can I ask what does for 'key in set(shopping_cart) | set(shopping_cart_2)' does ? I wanna learn please – elsonn Aug 06 '22 at 16:50
  • `set(shopping_cart) | set(shopping_cart_2)` is the union of the two sets of keys. `for key in `(that set) iterates over that set, assigning each key to the variable `key`. – Samwise Aug 06 '22 at 17:25
  • As an addendum, `set(shopping_cart) & set(shopping_cart_2)` would be the _intersection_ of the two sets. – Chris Aug 06 '22 at 17:47
  • Also possibly worth explaining that `shopping_cart.get(key, 0)` is retrieving the value at key `key` _or_ returning `0` if the key is not present. As opposed to `shopping_cart[key]`. – Chris Aug 06 '22 at 17:50
0

Yes! You can do that.

There is a data structure type Set in python which is just the counterpart of mathematical concept Sets. You can use this to create a list of all the distinct keys from both the dictionaries.

shopping_cart = {'milk': 1, 'eggs': 1, 'bread': 2}
shopping_cart_2 = {'milk': 1 ,'coke': 3, 'oyster sauces': 1}
d1k = set(shopping_cart)
d2k = set(shopping_cart_2)
allKeys = d1k | d2k

Now you can use a simple if..else statement inside of a for loop.

for i in allKeys:
    if i not in shopping_cart_2:
        pass
    elif i not in shopping_cart:
        shopping_cart[i] = shopping_cart_2[i]
    else:
        shopping_cart[i] += shopping_cart_2[i]

The above code creates the final required dictionary by modifying the original shopping_cart dictionary. Now you can delete the shopping_cart_2 dictionary.

del shopping_cart_2