Keeping track of the produce( for loop question )


I don't know why the printing sequence to the console will become: orange, pear, banana, apple, either: banana, apple, orange, pear.

#price of fruits
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5, 
    "pear": 3

#stock of fruit
stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15

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


Dictionaries are unordered, that is they do not have any memory of the order the items are inserted into the dictionary. We cannot predict which order they will print out.


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