Directory and For-Loops // Python2 Beginner course

Hey guys, I have a question… The code is simple but I dont understand why the output starts with orange in the console. Does Python start with floats at first??? Have a look. Thanks.

prices = {"banana": 4, "apple": 2, "orange": 1.5, "pear": 3}
stock = {"banana":6, "apple":0, "orange": 32, "pear": 15}

for x in prices:
  print x
  print "price: " + str(prices[x])
  print "stock: " + str(stock[x])

In python 2, dicts are unordered meaning that the order of insertion is not maintained. If such behaviour is desired then an OrderedDict should be used. Also note that as of python 3.7 the order of insertion is guaranteed on a normal dict.

from collections import OrderedDict
prices = OrderedDict([('banana', 4), ('apple', 2), ('orange', 1.5)])

for i in prices:
    print prices[i]

outputs

4
2
1.5
1 Like