Divide the values of two dictionaries in python

Viewed 12778

I have two dictionaries with the same keys and I would like to do division on the values to update or create a new dictionary, keeping the keys intact, with the quotient as the new value for each of the keys.

d1 = { 'a':12 , 'b':10 , 'c':2 }
d2 = { 'a':0 , 'c':2 , 'b':5}
d3 = d2 / d1

d3 = { 'a':0 , 'b':0.5 , 'c':1 }

Aside from iterating through the key, value pairs and creating ordered lists of the values, then dividing, I'm not sure how to do this. I was hoping for a more elegant solution.

4 Answers
Related