Problem with Python dictionaries

Problem with Python dictionaries

by Marcos Martínez -
Number of replies: 1

Hello, i have a problem with questions in which i ask the students to create a dictionarie, since each time a test case is run the resulting dictionarie appears in random order (and thus i't most often doesn't match expected output). I wonder why this happens, since ipython/spyder/jupyter naturally keep the order in which elements were introduced to the dictionary (in spite of a dictionary not actually having internal order).

Thanks for your attention.

In reply to Marcos Martínez

Re: Problem with Python dictionaries

by Richard Lobb -

Prior to Python 3.6, dictionaries were arbitrarily ordered. But since 3.6, entries are in insertion order. see https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6

It sounds like your jobe server is running a version of Python older than 3.6. I suggest you ask your system administrator to update jobe or at least the version of Python it's running. 

Before Python 3.6 we used to deal with this problem by having our tests print out the dictionary in sorted order, e.g. print(sorted(dictionary.items()))