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


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/1/3?curriculum_id=4f89dab3d788890003000096


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:


orange
prices: 1.5
stock: 32

pear
prices: 3
stock: 15

banana
prices: 4
stock: 6

apple
prices: 2
stock: 0

None


## 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).


#2

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)


#3

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