New entries - why does it print in the order it does?


new entries

My code completes the assignment, however I'm not sure why it prints in the order that it does.

I expected it to print in the order I added the dishes to the menu. Instead it prints
{'Spaghetti Bolognese': 12.45, 'Chicken Alfredo': 14.5, 'tatter tots': 60.9, 'Pizza Hawaii': 13.07}

It doesn't even sort by price or alphabetically? Does anyone know the reason?

menu = {} # Empty dictionary
menu['Chicken Alfredo'] = 14.50 # Adding new key-value pair
print menu['Chicken Alfredo']

# Your code here: Add some dish-price pairs to menu!
menu["Spaghetti Bolognese"] = 12.45
menu["Pizza Hawaii"] = 13.07
menu["tatter tots"] = 60.90

print "There are " + str(len(menu)) + " items on the menu."
print menu


you can look at the implantation of dictionary's in python to see why a certain order is produced, but why would you care? Python doesn't give you control over the order

If you want order, use lists (or tulpes)


Thanks! I guess this was really a question out of curiosity then anything. I appreciate the timely feedback!


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