How to show the keys of a dictionary in the order in which they were inserted

3

I'm trying to show in the idle the keys and their values, but I'd like to do it using the default order, not chance.

For example, I have this dictionary:

months = {'January':31,'February':28,'March':31,'April':30,'May':31,'June':30,
'July':31,'August':31,'September':30,'October':31,'November':30,'December':31}

But your content is not shown in order, ie "January" and then "February", etc.

How can I do this?

    
asked by anonymous 10.11.2014 / 10:06

1 answer

3

Note: Since no one answered, I decided to give a personal and formal response.

This problem arises because the standard types dict in Python are mutable and disordered, ie the initial insertion order of chaves:valores is not maintained during the dictionary life cycle.

One of the best workarounds for this problem is to use the OrderedDict of the collections module. p>

For more documentation and help, check out these posts:

10.01.2015 / 02:54