Output composite structures in Python in Google Colab

Asked

Viewed 161 times

4

I have this list called "brazil" and I wanted to understand why when I asked for it in Google Colab Notebook calling its respective variable, the list is presented in a certain order and when I print it out, it appears with dictionaries that are inside it in an inverted way. So my doubts are:

  1. This happens due to some update of Python 3.9 or something?
  2. How I check the version of Python running on Google Colab Research?
  3. Does it happen because of some optional sort parameter whose default value causes it? If not, what would happen?
  4. The same thing happened to someone else?

Caveats:

  • Yes, I have tested this in other code editors including IDLE 3.8.3, so I should worry about updating Python and its respective IDLE to see if the problem persists?
    • And to make it a little more explicit: testing in IDLE the result was as "expected", that is, the order of the dictionaries in the list was maintained after the variable call.

Follows the code:

estado1 = {'uf':'Rio de Janeiro', 'sigla':'RJ'}
estado2 = {'uf':'São Paulo', 'sigla':'SP'}

brasil = []
brasil.append(estado1)
brasil.append(estado2)

print(brasil) # A
brasil # B
--------------------
output:

[{'uf': 'Rio de Janeiro', 'sigla': 'RJ'}, {'uf': 'São Paulo', 'sigla': 'SP'}] # A

[{'sigla': 'RJ', 'uf': 'Rio de Janeiro'}, {'sigla': 'SP', 'uf': 'São Paulo'}] # B

Código acima no Google colab com os resultados

The same goes for dictionaries alone:

Chamando as variáveis estado1 e estado2 sem utilizar o print, seus outputs saem na ordem inversa à digitada na assinatura da variável

In IDLE (the code is the same from above):

>>> estado1
{'uf': 'Rio de Janeiro', 'sigla': 'RJ'}
>>> estado2
{'uf': 'São Paulo', 'sigla': 'SP'}
>>> print(brasil)
[{'uf': 'Rio de Janeiro', 'sigla': 'RJ'}, {'uf': 'São Paulo', 'sigla': 'SP'}]
>>> brasil
[{'uf': 'Rio de Janeiro', 'sigla': 'RJ'}, {'uf': 'São Paulo', 'sigla': 'SP'}]
>>> 

So this is basically my curiosity. I thank to whom to answer!

1 answer

4


This happens because the resource of printing beautification is activated.

The Google Colab Notebook is a web application that lets you write Python code in your browser.
The laptops of Colab allow you to combine executable and rich text code into a single document.

The laptops of Colab are laptops of Jupyter, staying at Colab, which in turn use an implementation Python other than those used in the shell versions of the language, Ipython.

The Ipython has cosmetic features that distinguish it from other language implementations, Cpython, Rpython and other implementations, has a browser-based notebook interface with support for code, text, mathematical expressions, embedded graphics and other media.

The same behavior of your example, sort the dictionary keys mentioned at the end, with another code:

import pprint       # Importa o módulo pprint

estado1 = {'uf':'Rio de Janeiro','sigla':'RJ'}
estado2 = {'uf':'São Paulo', 'sigla':'SP'}

brasil = []
brasil.append(estado1)
brasil.append(estado2)

print(brasil) # A
pprint.pprint(brasil) # C <-- Troquei a menção ao objeto pela chamada pprint.pprint()

Resulting:

[{'uf': 'Rio de Janeiro', 'sigla': 'RJ'}, {'uf': 'São Paulo', 'sigla': 'SP'}]
[{'sigla': 'RJ', 'uf': 'Rio de Janeiro'}, {'sigla': 'SP', 'uf': 'São Paulo'}]

In this test the method pprint.pprint( objeto , stream = None , indent = 1 , width = 80 , depth = None , * , compact = False , sort_dicts = True ) prints the formatted representation of the object in stream, followed by a new line. If stream for None, sys.stdout will be used. The parameter sort_dicts = True indicates that the action in the absence of pprint() is that when printing dictionaries order them by key.

It turns out that when an object is mentioned or returned in the console Ipython by default this uses the module pprint to display your representation, thus ordering dictionaries by the keys.

To enable/disable this and other behavior the Ipython provides magic commands and among them is the %pprint activating/deactivating the printing pprint.

In your code:

#Chamado penas uma vez em um console aparte para desligar o pprint para os outros consoles

%pprint

Resulting:

Pretty printing has been turned OFF

And for subsequent consoles:

estado1 = {'uf':'Rio de Janeiro','sigla':'RJ'}
estado2 = {'uf':'São Paulo', 'sigla':'SP'}

brasil = []
brasil.append(estado1)
brasil.append(estado2)

print(brasil) # A

brasil # B

Resulting in:

[{'uf': 'Rio de Janeiro', 'sigla': 'RJ'}, {'uf': 'São Paulo', 'sigla': 'SP'}]
[{'uf': 'Rio de Janeiro', 'sigla': 'RJ'}, {'uf': 'São Paulo', 'sigla': 'SP'}]

Test in Google Colab

Browser other questions tagged

You are not signed in. Login or sign up in order to post.