Python Array

Cameron Simpson cs at zip.com.au
Thu Mar 31 02:41:15 EDT 2016


On 30Mar2016 22:53, Stephen Hansen <me+python at ixokai.io> wrote:
>On Wed, Mar 30, 2016, at 10:34 PM, tdsperth at gmail.com wrote:
>> as you can see the option element was added third but is the first one
>> displayed.
>>
>> Is this just standard - I am using Python 3.5
>
>The order of items in dictionaries is based on the hash value -- which
>while stable, should be considered random and not reliable. If you want
>to maintain insertion order, consider using an OrderedDict:
>
>https://docs.python.org/3.5/library/collections.html#collections.OrderedDict

Alternatively, you can decide to not care, and to present the dictionary keys 
in some arbitrary order of your chosing when printing output (commonest example 
is probably lexically sorted).

On the other hand, if you really do want the insertion order then OrderedDict 
is the way to do, as several others have mentioned.

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list