Why does for loop not start from first key-value pair?



Hello, I was just wondering why this error message comes up
"Oops, try again." Check what your code prints for orange. It doesn't look quite right! since the output for orange looks absolutely normal to me:

prices: 1.5
stock: 32

prices: 3
stock: 15

prices: 4
stock: 6

prices: 2
stock: 0


## supermarket itiniery ## 

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 "prices: %s" % prices[key]
    print "stock: %s\n" % stock[key]

I also wanted to ask, why does the for loop start listing the key-value pairs in such an order? I expected it to either list them in the order they were stored (banana, apple orange, pear) or randomly, but not starting from the third item in itinerary and then listing them in order (orange, pear, banana, apple).


a product has a price: not prices:

dictionaries are unodered by nature, if you want to understand how this implemented, study the implementation of dictionaries and hash tables in python.

(note, for other people who will say: it works in my python IDLE, yes, in python 3.6 (the latest version) dictionaries preserve order)


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