2 dictionaries scrolling

I would like to go through these dictionaries to find out how much money you will make if you sold all the food in the warehouse. Print this value in the console!

prices = {"banana": 4, "apple": 2, "orange": 1.5, "pear": 3}

stock = {"banana": 6, "apple": 0, "orange": 32, "pear": 15} all_total =

-3
source share
2 answers

Like this:

sum(prices[key]*value for key,value in stock.iteritems())
+1
source

As long as dictionaries have the same keys, you can use:

all_total = 0
for item in prices:
   all_total += prices[item] * stock[item]
0
source

Source: https://habr.com/ru/post/1627214/


All Articles