Reputation: 29
prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}
total = 0
for item in prices:
a=prices[item]*stock[item]
total=total+a
print total
so here is my code, i intentionally wrote the print total within the for statement, so it print out the order of calculation for me and the number goes like this 48.0 93.0 117.0 117.0, so after the basic calculation the first value that the statement churns out is the total of orange the second is the total of pear the third is the banana and the last is the apple
now i am curious under what principle is this precedence of calculation carried out cause it seems to me that it started with the largest number in dic_stock and calculated in a descending manner, i do aware this is just a superficial observation,there must be a reason why computer did this
can anyone tell me why :)
Upvotes: 1
Views: 105
Reputation: 213351
Dictionaries are not ordered. So, the ordering in which you get the key-value pair is not fixed.
If you need to define insertion order on your dict, you need to use collections.OrderedDict
Upvotes: 5