0

I installed the Anaconda 3 and I have a problem, I have read that dict is ordered automatically by the keys, but this is not happening in my installation

In the book that I am reading there is this example, wrote in a jupyter notebook:

enter image description here

this is what supposed to happen enter image description here

I tested on https://colab.research.google.com and I got the output ordered, so is my anaconda installation the problem? is this just a visual representation?

Sorry for my english, thanks in advance

Devmotonio
  • 53
  • 1
  • 7

3 Answers3

7

If you want your dictionary to be sorted alphabetically, you can do the following:

>>> print(dict(sorted(dic_estados.items())))
{'AM': 'Amazonas', 'BA': 'Bahia', 'MG': 'Minas Gerais', 'PR': 'Parana', 'RN': 'Rio Grande do Norte'}
Francisco
  • 10,918
  • 6
  • 34
  • 45
1

Here is how you can achieve the desired output:

dic_estados = {"PR":"Parana","MG":"Minas Gerais","BA":"Bahia","RN":"Rio Grande do Norte","AM":"Amazonas"}
sorted_dic = {k : v for k, v in sorted(dic_estados.items())}

print(sorted_dic)
#{'AM': 'Amazonas', 'BA': 'Bahia', 'MG': 'Minas Gerais', 'PR': 'Parana', 'RN': 'Rio Grande do Norte'}
pakpe
  • 5,391
  • 2
  • 8
  • 23
0

as everybody pointed out the dict are not ordered by the keys, the output in the jupyter notebook uses a function called pprint to show ordered by the keys, and for some reason on my installation this pprint is not working. is not an error

Thanks all

Devmotonio
  • 53
  • 1
  • 7