educative.io

Dictionaries - Learn Python 3 from Scratch

In the example “Creating a dictionary” why are the key/value pairs printed in a different order than they are given in the code? This is not explained below the example.

Again in the example below “Creating a dictionary” called " The dict() constructor" the key/value pairs are printed in a different order than they are given in the code and a different order than they are printed in the previous example. Once again this is not explained or even noted below the code. I did notice that the key/value pairs are printed in ascending value in this example, maybe this is why the order is different than the order that was given in the code.

Aside from this confusion this is an excellent course so far.

It states in the lesson that the dictionary is an unordered data structure. What this implies is that when you access it in a loop or print the whole dictionary, elements will be accessed randomly. Hence, the order in which we inserted them will not be preserved as they are not stored linearly. The same thing happens with sets.
A note on this has also been added to the lesson so do check it out!

Rauhaan | Developer Advocate
educative.io