Why aren’t the values added to the dictionary in order?


#1

Question

Why aren’t the values added to the dictionary in order?

Answer

In Python, a dictionary doesn’t add your items in the order you might expect. If we added 3 dishes, Burger, Salad, and Sammich, we’d expect them to appear in that order. But we’d be wrong!
There’s an advanced reason behind it, and like anything in programming, it’s because of efficiency. For now, just know that adding things to a dictionary does not add it in an order you’d expect, or necessarily at the end of the dictionary.


FAQLearn Python: Python Lists and Dictionaries - New Entries
#2

Could someone please answer the question? The current answer only states the obvious without giving any hints as to which algorithm is used


#3

That’s the point. You should not rely on any order.
Python is free to do whatever it wants with that. (3.7 and later is not though, because it was added to the language specification that insertion order is kept)