Orange gets printed first on the console?


10. A Day at the Supermarket - 9. Something of Value

The order in which the keys get printed is: Orange, pear, banana, apple.

It should be: Banana, apple, orange, pear

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:
    total += stock[key]*prices[key]

print total


why? There is no reason for dictionaries to be ordered, given they are accessed by key and not by index, so not ordering them is more memory efficient.

from python3.6 on-wards dictionaries are ordered, you can read about it here. Good luck, tough read


why is the order orange, pear, banana, apple every time? What makes the interpreter start at orange?


then we would have to look how dictionaries/hash tables are implemented into python. I don't think this would be wise at this point in the course

I know, frustrating, but going into to deep water you might drown.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.