Why the order of this output?


#1



Keeping Track of the Produce


the output shows:
orange
price: 1.5
stock: 32
pear
price: 3
stock: 15
banana
price: 4
stock: 6
apple
price: 2
stock: 0


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


prices={
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

stock={
    "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]


#2

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?


#3

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


#4

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:

http://docs.python.org/2/tutorial/datastructures.html#dictionaries


#5

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