Multiplying and then summing values from two dictionaries (prices, stock)

Question:

I need to multiply the values from each key and then add all the values together to print a single number. I know this probably super simple but i’m stuck

In my mind, I’d address this with something like:

for v in prices:
total = sum(v * (v in stock))
print total

But something like that isn’t going to work 🙂

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

stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15 }
Asked By: Rhys Isterix

||

Answers:

If you would have known, how to iterate through a dictionary, index a dictionary using key and comprehend a dictionary, it would be a straight forward

>>> total = {key: price * stock[key] for key, price in prices.items()}
>>> total
{'orange': 48.0, 'pear': 45, 'banana': 24, 'apple': 0}

Even if your implementation of Python does not provide Dictionary comprehension (< Py 2.7), you can pass it as a List Comprehension to the dict built-in

>>> dict((key, price * stock[key]) for key, price in prices.items())
{'orange': 48.0, 'pear': 45, 'banana': 24, 'apple': 0}

If you don;t want compatible between 2.X and 3.X you can also use iteritems instead of
items

{key: price * stock[key] for key, price in prices.iteritems()}

If you want a single total of the result, you can pass the individual products to sum

>>> sum(price * stock[key] for key, price in prices.items())
117.0
Answered By: Abhijit

You could use a dict comprehension if you wanted the individuals:

>>> {k: prices[k]*stock[k] for k in prices}
{'orange': 48.0, 'pear': 45, 'banana': 24, 'apple': 0}

Or go straight to the total:

>>> sum(prices[k]*stock[k] for k in prices)
117.0
Answered By: DSM

I’m guessing you’re on codeacademy? If so just do this:

total = 0
for key in prices:  
    prices = 53
    stock = 10.5
    total = prices + stock
print total  

Unlike what the instructions said you would have to add all the values together before multiplying them and adding them to total. Hope this helps.

Answered By: The Rising Sun
total = 0   
for key in prices:
    print prices[key] * stock[key]
    total += prices[key] * stock[key]
print total
Answered By: Samko

Correct answer for codeacademy according to task description:

 prices = {
       "banana" : 4,
       "apple"  : 2,
       "orange" : 1.5,
       "pear"   : 3,
   }
   stock = {
        "banana" : 6,
        "apple"  : 0,
        "orange" : 32,
        "pear"   : 15,
    }

    for key in prices:
        print key
        print "price: %s" % prices[key]
        print "stock: %s" % stock[key]

     total = 0
     for key in prices:
        value = prices[key] * stock[key]
        print value
        total = total + value
    print total   
Answered By: Alexander Petrov

I wrote the following code and it worked.
for key in prices:

print key
   print "price: %s" % + prices[key]
   print "stock: %s" % + stock[key]

for key in prices:
value = prices[key]*stock[key]
print value
total = total + value
print total

Answered By: Deirdre Brennan

Quick and general answer.

If you are looking for an efficient and fast way to multiply the elements (values) of two dictionaries together, do use dict comprehension.
Be sure the two dictionaries have the same keys.
You could use this one-liner:

new_dict = { key : value * dict2[key] for key,value in dict1.items() }
Answered By: Andrea Bragantini
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.