19

I am trying to pretty print a dict in Jupyter Notebook.

I am using the following:

import pprint
stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
stuff.insert(0, stuff[:])
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(stuff)

However upon pressing shift+enter, no [out] cell appear (i.e. I can't see the pretty printed output)

enter image description here

Any idea why this is so/ what should I change in order to see the pretty printed output?


Edit: Actually this is a python 2.7 problem - it works fine in 3.x. Have anyone tried it on python 2.7 and seen it work?

Harrison Grodin
  • 2,253
  • 2
  • 19
  • 30
jim jarnac
  • 4,804
  • 11
  • 51
  • 88

3 Answers3

7

Whatever you develop is correct. The only possible reason could be, Jupyter Notebook is unable to connect to the server. If you see connecting to Kernal/server like the image below in the toolbar, try to refresh the connection or reload the page.

enter image description here

I used the same code and I am able to see the output. check the image below.

enter image description here

0

I had this problem with Python 3, in my case solved removing an AD block extension.. somehow it was blocking the AJAX request that set the output..

rodfersou
  • 914
  • 6
  • 10
0

The title is generic to more than dict in Jupyter, so i give a generic answer.

When printing, change this

print(longValue, nextValue)

to this (have to try with other values than 30)

space = (30-len(longValue)*" "
print(longValue,space,nextValue)

That will change this

enter image description here

Into this

enter image description here

Punnerud
  • 7,195
  • 2
  • 54
  • 44