Reputation: 17
OrderedDict([('red', 10.0),('blue',45.5),('green',34.4)]
xList[(u'blue',1.5,u'car'),(u'green',1.9,u'bike'),(u'red', 10,u'boat')]
How do I loop through the orderedDict and List updating the ordedDict values by mulitplying the value by xList[1]
?
Hope that makes sense I am struggling with this for the last few nights and any help will be greatly appreciated.
Ideally I want to get back:
OrderedDict[('red',100),('blue',68.25),('green',65.36)]
Upvotes: 1
Views: 73
Reputation: 180401
from collections import OrderedDict
d = OrderedDict([('red', 10.0),('blue',45.5),('green',34.4)])
xList = [(u'blue',1.5,u'car'),(u'green',1.9,u'bike'),(u'red', 10,u'boat')]
for tup in xList: # iterate over each tuple
if tup[0] in d: # make sure key is in the dict
d[tup[0]] *= tup[1] # multiply current value by the second element of the tuple
print(d)
OrderedDict([('red', 100.0), ('blue', 68.25), ('green', 65.36)])
Upvotes: 1