Why the order of this output?


Keeping Track of the Produce

the output shows:
price: 1.5
stock: 32
price: 3
stock: 15
price: 4
stock: 6
price: 2
stock: 0

why is it in that order as to banana, apple, orange, pear? with is the order of the keys

    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3

    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15

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


Look here:

This line is sort of lengthy. There's a much simpler way at printing key :thinking:. Do you know what it is? :slight_smile:

Why use string formatting when only printing one variable?


yeah, just print key... definitely not my question though.


Anyways, our dictionaries are stored in no fixed order, and won't be printed in order. That's sort of the way Python does it. That doesn't mean our list will be printed in a different order every single time we refresh the page and print again. Do you know what I mean?

More about dictionaries:



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