Skip to content
Home » Jupyter Notebooks: How to print multiple outputs in a cell

Jupyter Notebooks: How to print multiple outputs in a cell

On my list of things that frustrate me when using notebooks is the fact that you do not see the output of every expression of a cell by default. For example, during an exploratory analysis phase, I want to see the result of two pandas operations or function within the same cell, because they belong to each other.

Luckily, there’s a Jupyter setting that you can change to print multiple outputs.

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'

The ast_node_interactivity setting allows you to choose which results are shown as outputs. By setting it to ‘all’, every assign and expression will be shown. If that’s too much for you, here are the other options. I demonstrate what every option will return with two chunks of example code.

5 + 5
3 + 3
my_var = 6 + 6
10 - 10
for i in range(1,5):
    i * 2
  • last_expr: only print the last statement if it is an expression (default) — returns nothing twice
  • none: print nothing — returns nothing twice
  • last: prints the last expression, even if it is in a loop, without a nextline character. — returns nothing and ‘2468’
  • last_expr_or_assign: Print the last expression or assignment. Only for simple variables. It doesn’t work when you assign a value to a position in an array. — returns ’12’ and nothing
  • all: prints all expressions without a nextline character. — returns ‘106’

As you can see. Not all options are useful for simple expressions because the output of all expressions gets concatinated. Yet when you work with data frames, they will be aligned vertically, below each other.

In Google Colab, you cannot change the setting to last_expr_or_assign — it will crash your kernel. There’s a simple workaround: by installing the latest version of IPython, you will get access to the option.

!pip install -U ipython 

Another problem solved!

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.

57 thoughts on “Jupyter Notebooks: How to print multiple outputs in a cell”

  1. With havin so much content do you ever run into any issues of plagorism
    or copyright infringement? My blog has a lot of completely unique content I’ve either
    created myself or outsourced but it looks like a lot of it is popping it up all over the web without my agreement.
    Do you know any techniques to help reduce content from being ripped off?
    I’d genuinely appreciate it.

    Take a look at my site :: Naomi

  2. There’s a micro USB connector on the right aspect,
    and an SD card slot on the left protected when not in use by a rubber plug.
    Identical to Tesco, the first obtainable supply slot for ASDA is on the 14th of April, although, the supermarket at the moment appears to
    be having trouble with their webpage as the grocery section is presently down. It looks like you will be waiting until around the center/end of April for a delivery slot.
    If you do not like the slot machine method, you can browse eating places
    by numerous classifications or view a listing of Urbanspoon listings close
    to your present location. Once on the web page, type “Cherry Master slot machine” into
    the search discipline at the highest of the web page and press
    enter to bring up the listings. The RTP fee reaches 95.53%.
    Gamblers are really useful to attempt to follow the Flaming Hot slot demo to develop their own strategies for the game.
    On this paper we only outline a slot filling drawback in Dress category
    domain for simplification. This is making it onerous to get a supply slot, with wait times
    now measured in weeks somewhat than days.

  3. Could I receive your authorization to copy your article? It has been an invaluable resource, and I’m truly grateful for the time and effort you’ve invested in sharing your expertise. Can you visit my website too?? at Kampus Terbaik Thanks! ID : CMT-SA1CCFV81ANJOWEF4M

  4. SightCare clears out inflammation and nourishes the eye and brain cells, improving communication between both organs. Consequently, you should expect to see results in as little as six months if you combine this with other healthy habits.

  5. urveillez votre téléphone de n’importe où et voyez ce qui se passe sur le téléphone cible. Vous serez en mesure de surveiller et de stocker des journaux d’appels, des messages, des activités sociales, des images, des vidéos, WhatsApp et plus. Surveillance en temps réel des téléphones, aucune connaissance technique n’est requise, aucune racine n’est requise.

Leave a Reply

Your email address will not be published. Required fields are marked *