Testing list sequence question

Roel Schroeven roel at roelschroeven.net
Sat Mar 4 11:59:32 EST 2023


Gabor Urban schreef op 4/03/2023 om 17:38:
>   Hi guys,
>
> I have a strange problem that I do not understand. I am testing function
> which returns a dictionary. The code should ensure that the keys of the
> dictionary are generated in a given order.
>
> I am testing the function with the standard unittest module and use the
> assertListEqual statement to verify the sequence. Sometimes this test
> fails, sometimes passes without any change neither in the code, nor in the
> testcase. I am using "list(myDict.keys())" to create the list of the keys
> of the dictionary.
>
> I am running Python 3.3 on MS Windows. Any idea why is this?
Prior to Python 3.6 (de facto) and Python 3.7 (officially), there was no 
guarantee at all about the order of keys in a dict. If you need the keys 
to be in the same order they were inserted, either use 
collections.OrderedDict or upgrade to Python 3.7 or later.

-- 
"Life ain't no fairy tale
Just give me another ale
And I'll drink to Rock 'n Roll"
         -- Barkeep (The Scabs)



More information about the Python-list mailing list