Errors and Execution of the same code in Jupyter notebook through Anaconda

I teach python today using Jupyter Notebook by Anaconda. Because I cant’ attach a screenshot so here is my code:

fruits = [“apple”, “banana”, “cherry”, “apple”]
fruits.append(‘lettuce’)
print(fruits)

The return is [‘apple’, ‘banana’, ‘cherry’, ‘apple’, ‘lettuce’]

The next code cell is :

fruits.append(‘pear’)
print(fruits)

The return is [‘apple’, ‘banana’, ‘cherry’, ‘apple’, ‘pear’, ‘pear’, ‘pear’]

And this is a brand new notebook, but the first line number is shown as line 8, the second code cell is shown as line 11.

The return and line numbers are incorrect. In another longer notebook in the class, I append lettuce to the list, every time I execute the same code, one more “lettuce” item is added to the list. I execute six time, then six “lettuce” items are appended. It should be only one. I tried Google Colab, the returns are correct, only one “lettuce” no matter how many times I execute the code and both “lettuce” and “pear” are in the list.

Is it the Python’s problem or Anaconda? Are there any new rules now of Python?

Do you mean Anaconda Cloud? In other words, do you see ‘Home | Cloud: Data Science an Python Tools from Anaconda’ as the name of the tab from where you launch Jupyter?

Do you mean line numbers or cell execution numbers?

I ask about cell execution numbers because if you are in the same session and you use a kernel that is already open, the cell execution numbers won’t be correct. Did you shut down all the kernels and try restarting? Did you make sure you didn’t have multiple notebooks tied to the same kernel? (This is my main suspicion about what is happening.)

Another idea….

I also ask about cell execution numbers because last year there was an issue last year where the cell execution numbers were off because the ‘Anaconda Assistant’ was causing invisible executions; however, one of the suggestions was updating there and I don’t think you can update Anaconda Cloud(?).


Any chance you have real-time collaboration active?


I’m trying Anaconda Cloud right now (JupyterLab 4.0.9 & Jupyter Notebook 7.0.6) and everything looks fine, but I only have old kernels I made a while ago. What kernel are you using with what JupyterLab version? Or Jupyter Notebook version?


You can rule out something by testing elsewhere perhaps? Go here and click ‘`launch binder`’ and see if you have the same issues in the session there?


While I am mentioning GitHub gists…

There are ways to share notebooks using Github’s gist. It will share it in pre-run form so we can see what happened and have the option use MyBinder to test it & compare.
Example rendered via nbviewer. (Source notebook actually stored as Gihub gist , at which nbviewer was pointed.)

What you shared now would have benefitted from taking advantage of how you post code here as block code formatting, see here about ‘Block code formatting’ for details.

Hi Fomightez, thank you so much for your prompt response. I learned a lot from your answer. I used Anaconda Navigator. I opened the Jupyter Notebook there. The version is 7.3.2. I didn’t see ‘Home | Cloud: Data Science an Python Tools from Anaconda’.

Sorry about the confusion. It is actually cell execution numbers instead of line numbers.

I am not sure I have real time collaboration active. I didn’t turn it on.

But the problem is still there. Today I opened one notebook, and executed below:

fruits_list.append(‘lettuce’)
print(fruits_list)
fruits.add(‘pear’)
print(fruits)

the return is [‘apple’, ‘banana’, ‘cherry’, ‘apple’, ‘lettuce’, ‘lettuce’, ‘lettuce’, ‘lettuce’, ‘lettuce’, ‘lettuce’]

It is my first time appending “lettuce” in my whole notebook. It has so many “lettuce” items because I execute this code block multiple times. Last year when I used this jupyternotebook, I executed the same code block multiple times, it appended “lettuce” only once, such as [‘apple’, ‘banana’, ‘cherry’, ‘apple’, ‘lettuce’]

So, I opened another notebook under the same kernel, and run the same code in the first code block, this time, it is only one “lettuce” item appended, no matter how many times I executed this block. Please see the code in my initial post.

Then I tried to append “pear” to the new list that includes lettuce. the return is The return is [‘apple’, ‘banana’, ‘cherry’, ‘apple’, ‘pear’, ‘pear’, ‘pear’]. Please see the code in my initial post.

Below is the AI assistant’s answer for multiple lettuce items appended to the list:
If you’re seeing multiple “lettuce” items in the output, it’s likely because the code `fruits_list.append(‘lettuce’)` is being executed multiple times. This can happen in a Jupyter notebook environment if: 1. You’ve run the cell containing this code multiple times without restarting the kernel 2. The code is inside a loop that’s executing multiple times 3. The cell has been executed in an interactive session where state is maintained between executions Each time the append statement runs, it adds another “lettuce” to the existing list. Unlike creating a new list, the append method modifies the existing list in-place, so repeated executions will keep adding more items to the same list. To fix this, you could: - Restart the kernel and run your cells in order - Create a new list before appending to it - Use debugging techniques to check where the multiple executions might be happening

If so, when I append “pear” to the list, it removes “lettuce” and appends three “pears”? It might be I executed the code three times, but why lettuce is removed?

Thanks for more information on what you are using. That part is clearer; however, in the future mentioning the version of Python would be best, too. Jupyter and Python versions are not locked in any relative way and so just because you say version 7.3.2 of Jupyter Notebook, there’s no way to tell what version of Python is involved. Here it is probably not that important since the apparent issue would be so fundamental that I doubt a Python version would be released with such an issue.

However, the other aspects of your post still need some work. Please read about what I said about how to share formatted, useable code and actual notebooks, such as via GitHub gists. Little of what you have provided is useable or clear. Please read Getting good answers to your questions . Especially read the section ‘Help others reproduce the problem’ in the last link there.

This is unclear what this is doing here?

Finally, I provided other places to run the code and try things (you can also use Anaconda Cloud that I referenced when I was wondering what you were describing in your original post) so you can tell if it is your particular system that only has the problem or a particular version of Jupyter Notebook or Python. You seem to have not investigated any of that.

Additionally, if you are just looking to learn Python those resources may be more convenient so that you can keep your own machine clear for when you know what you are doing and need secure computing.


It seems the second attempt to append is the one with issues? Or sometimes it is the first?

Thank you for your instructions. The resources you shared are great for Python learning. I executed my codes again, some problems disappeared, and now only one problem exists. It has been the key problem that confuses me. And I tried it in Anaconda Cloud too, and it is the same.

I attached my codes in the list below. On August 4 I found this problem, and I tested it again on August 5 by opening a new book. In the August 4 notebook, Cell 41 and Cell 44, you see I appended “dragonfruit” for the first time and I executed it only ONCE but three “dragonfruit” items are appended.

So I opened a brand new notebook on August 5. I appended “lettuce” to the list, no matter how many times I execute it, there is always only one “lettuce” in the list. The next code block is another story. Every time I execute this code block, one “pear” item is appended. I executed three times, three “pear” items appended.

August 4

https://nbviewer.org/gist/lixiao321/1b10210e1d904d34738ae7dca85246b7

August 5

https://nbviewer.org/gist/lixiao321/dfd82a672d5ff1ee9d66167b7f491377

I’m glad to hear that some of the issues disappeared for you.

I don’t see what you report though and what you provided isn’t proving it.

All I see is a lot of cells with out of order execution numbering and possible ‘hidden state’ issues.


Let’s start by considering the simplest notebook, the August 5th one. What I see is the first cell has an execution number of 17 and the second cell has an execution number of 15. Why not the first cell having the execution number of [1] and the second cell having the execution number of [2]? If you restart the kernel and run all cells, do you see that?

Here is what I see:
Aug 5 nb at nbviewer

No issue.

This is how notebooks are meant to be run. And if the numbering of the executed cells is not coming out like that when you run using ‘Kernel’ > ‘Restart Kernel and Run All Cells’, then come back and say that is what is happening.

Anything else is a potential problem because if the cells execution doesn’t start and increase and clearly show each one in order, we cannot discuss things because there’s no telling what you did in between. This is called the ‘Hidden State’ issue of notebooks. For example, in August 4th notebook it starts out at execution number [21] and then goes out of order up to around 29, then there is no visible cell [30]. What code did you run then? You could have clobbered/shadowed the built in append() method, and folks trying to help you would have no idea. There there is cell #31, then no cells #32 and #33. What code did you run then? See the issue. By the time you get to cell #41 and #44 where you say the issue is, I have no idea what you may have run as things aren’t in order top to bottom and cells run are not visible.

When I run your notebook, even out of order (against my better judgement), I don’t see the August 4th things like you see. See here.

As I said earlier, please read the section ‘Help others reproduce the problem’ here and look into making a minimal reproducible example. What you have provided isn’t it because as I have shown, running your code doesn’t do what you report. Also keep in mind with a Jupyter .ipynb as a vehicle for a minimal reproducible example you should always restart the kernel and run the code in order top to bottom (linearly) and save a fresh run with all executed cells visible.


There’s several convenient ways to move around cells if the order you have doesn’t suit things any longer.

GENERAL TIP: Restarting the kernel and running all cells in order linearly is a good habit to get in when developing Jupyter Notebook files. It will help you catch ‘hidden state’ issues earlier while you are more likely to still remember what you did and can fix it.