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

one point to note here is the extra spaces before the newline, which is a markdown tip to make sure the newline is displayed.

@fomightez do you have any similar tips for formatting JSON output?
the display(JSON(blob, expanded=True)) that I use sometimes is a bit clunky and the results are hard to navigate.

Are you using JupyterLab? (You can try it from here without needing to install anything.) There’s a viewer built-in that will let you view a JSON file rather nicely. See here for the features included in an animation. That animation also shows using JupyterLab cells.

Sorry, I actually don’t use JSON enough to have much of a knowledge base to draw on as to what works well dealing with it in an actual cell. I am a big dan of Pandas though and so I might try the suggestions in Analyzing JSON With Python if I had a lot to deal with.
There’s also this and this.

If those aren’t helping, you may want to start a new topic seeking tips.

On a related note, there’s pprint that is for producing aesthetically pleasing representations of your data structures, see here for one application and some resources.
The OP could use something like the following in combination with zooming out the browser window some degree and or hiding the navigation panel temporarily:

from pprint import pprint
pprint(people, width = 230)