JupyterLab Dictionary Content Output Format

Brand new to Python and this my first question on the forum (please be gentle rolleyes)

I have defined a dictionary with separate keys and values as lists, shown below;

people = { "first": ["Corey", "Jane", "John"], "last": ["Shafer", "Doe", "Millard"], "email":["CoreyMSchafer@gmail.com","JaneDoe@email.com","JohnM81@hotmail.com"] }

When i print the contents of the dictionary in “JupyterLab” the output is then returned in the following format;

{'first': ['Corey', 'Jane', 'John'], 'last': ['Shafer', 'Doe', 'Millard'], 'email': ['CoreyMSchafer@gmail.com', 'JaneDoe@email.com', 'JohnM81@hotmail.com']}

When i do the same using “Spyder” the output is returned with each keys contents list on the same line, as shown below;

[python]{'first': ['Corey', 'Jane', 'John'], 'last': ['Shafer', 'Doe', 'Millard'], 'email': ['CoreyMSchafer@gmail.com', 'JaneDoe@email.com', 'JohnM81@hotmail.com']}

How do i get “JupyterLab” to output the contents of the dictionary the same format as “Spyder” does, all on a single line? Or at least each keys list completely on each keys line.

Must be a setting in “JupyterLabs” perhaphs?

Many thanks for taking your time, any help is really appreciated.

There isn’t a true line break in the JupyterLab display. Zoom out in your browser and you should see the output of print(people) on one line. In Chrome, Zoom out is under View.

The following in the notebook will get closer to listing each key per line but still fails with email key in your provided example:

display(people)

But if you need to control display in Jupyter there may be other ways to accomplish what you seek? For example you can add in a little lightweight python code to do better handle things as you want. For example the following will place ’ keys list completely on each keys line’ as long as your JupyterLab window isn’t too narrow:

from IPython.display import Markdown, display
def printmd(string):
    display(Markdown(string))
printmd(str(people).replace("],","],  \n"))

With that code in Jupyter, you’ll see as output:

{'first': ['Corey', 'Jane', 'John'],
'last': ['Shafer', 'Doe', 'Millard'],
'email': ['CoreyMSchafer@gmail.com', 'JaneDoe@email.com', 'JohnM81@hotmail.com']}

(I employ that printmd() function in other answers here and here. Relying on the output now being markdown, I added in some markdown syntax to the string representation of the dictionary to control displaying parts more.)

1 Like

Thank you so much fomightez. I understand this must be very basic for accomplished Notebook users.

Appreciate you taking your time to advise