Keeping Track of the Produce (output order explanation)


Hello there, can anyone explain to me how do dictionaries function in detail and why is my for loop print the fruits in ascending order depending on stock left without me specifically asking it to? this doesn't make sense by any standpoint to my eyes coming from c/c++. Thanks in advance.
PS: here is the code for anyone wondering

prices = {"banana": 4, "apple": 2, "orange": 1.5, "pear": 3}
stock = {"banana": 6, "apple": 0, "orange": 32, "pear": 15}
for i in prices:
    print i
    print "price: %s" % prices[i]
    print "stock: %s" % stock[i]


Dictionaries are not ordered. Without going to extra measures we cannot control the order in which it prints. It will be different (nearly) every time.


yeah i read as much, i was more curious as to why this happens, since it's odd, especially you mentioning it will be different every time. Well i suppose i will have to finish the course before i jump into the technical stuff, thanks for the reply :slight_smile:


They are unordered because they do not have a sequential index. Dictionaries are more or less look-up tables that do not need order since we can retrieve by key name, as you see us doing above.