-2

I am writing a code which basically adds the values in similar items from both dictionaries, and prints rest items.

food = ["cheese pizza", "quiche","morning bun","gummy bear","tea cake"]

bakery_stock = {
    "almond croissant" : 12,
    "toffee cookie": 3,
    "morning bun": 1,
    "chocolate chunk cookie": 9,
    "tea cake": 25
}

new_di = dict.fromkeys(food, 1)
if new_di in bakery_stock:
     bakery_stock.update(new_di)
     print(bakery_stock)
Barmar
  • 741,623
  • 53
  • 500
  • 612
Rahul Patel
  • 96
  • 1
  • 4

1 Answers1

1
>>> from collections import Counter
>>> food = ["cheese pizza", "quiche","morning bun","gummy bear","tea cake"]
>>> bakery_stock = {
...     "almond croissant" : 12,
...     "toffee cookie": 3,
...     "morning bun": 1,
...     "chocolate chunk cookie": 9,
...     "tea cake": 25
... }
>>> new_di = dict.fromkeys(food, 1)
>>> Counter(bakery_stock) + Counter(new_di)
Counter({'tea cake': 26, 'almond croissant': 12, 'chocolate chunk cookie': 9, 'toffee cookie': 3, 'morning bun': 2, 'cheese pizza': 1, 'quiche': 1, 'gummy bear': 1})
jamylak
  • 128,818
  • 30
  • 231
  • 230