From PyScripter (3.6.4.0) REPL console:
*** Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)] on win32. ***
*** Remote Python engine is active ***
>>> d = {}
>>> d['B'] = 12
>>> d['A'] = 10
>>> d['C'] = 34
>>> d
{'A': 10, 'B': 12, 'C': 34}
That result makes us believe that Python sorts the key and doesn't preserve insertion order, whereas it's guaranteed from version 3.6.
Now let's run the exact same version of Python in a console, outside PyScripter:
Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {}
>>> d['B'] = 12
>>> d['A'] = 10
>>> d['C'] = 34
>>> d
{'B': 12, 'A': 10, 'C': 34}
Insertion order is preserved all right.
Why are the outputs different?
You need to disable the Pretty print output option in pyscripter:
Find it under Options > IDE Options > Python Interpreter.
The
pprint
module outputs dictionaries in sorted key order, ignoring the current ordering of the dictionary. From the documentation:It’s not a hack to match older Python version output, because before Python 3.6 the order depended on insertion and deletion order plus a randomised hash seed.
Instead, using
pprint
gives you nicer output when the output would otherwise become unwieldy, by using newlines and indentation, where the standard representation would just put everything on a single line.Your specific example doesn't really bring out the difference, a longer dictionary would make it clearer:
If you only occasionally need to check the specific item order of a dictionary, you could just leave the option enabled, and use
print(dictionary)
for those exceptional times.You could also pass the
sort_dicts=False
argument topprint()
, provided you call it manually; thepprint.pp()
function even makes this the default:Or you could ask the PyScripter project to use that option in their console implementation.