"list=list+['value'] and list.append('value')" adds 2 same values

I just begin to learn python. I’m using jupyter and i’m having a little problem with lists.

I define my list as ‘liste’.

liste=['a', 'b', 'c']

print(liste)
['a', 'b', 'c']

When i’m trying to add a new value this happens:

liste.append('d') or liste=liste+['d']

liste
['a', 'b', 'c', 'd', 'd']

It works when i type:

liste+['d']
liste
['a', 'b', 'c', 'd']

Did i do something wrong?

This:
liste.append(‘d’) or liste=liste+[‘d’]
effectively does the same thing twice. list.append modifies liste in place and returns None.
So the or statement is equal to the second expression, which is then evaluated and assigns the new list to liste. It just so happens that this new list, liste+['d'] would’ve had exactly the same value as the original liste after the single append operation. But it’s an assignment, and in Python the name liste could be re-assigned to anything:

liste.append(‘d’) or liste=["Another", "list", "entirely"]

Adding two lists is like calling list.extend, and list.extend on a length-1 list is essentially an append of the singleton list’s only element.

1 Like

Please the ‘preformatted text’ button to preserve proper code formatting.

Anyway, I can’t reproduce your result:

>>> liste = ["a", "b", "c"]
>>> liste.append("d")
>>> print(liste)
['a', 'b', 'c', 'd']
>>> liste = ["a", "b", "c"]
>>> liste = liste + ["d"]
>>> print(liste)
['a', 'b', 'c', 'd']

That is a SyntaxError:

>>> liste = ["a", "b", "c"]
>>> liste.append("d") or liste = liste + ["d"]
SyntaxError: cannot assign to expression

So that piece of code cannot be literally what they ran.

1 Like

Admittedly I didn’t install Jupyter to check it’s valid syntax in a worksheet. But I can’t think how else they got two 'd's in there, and as you say yourself you can’t reproduce it.

Jupyter worksheet spaghetti code is another possible cause, with parts of statements causing mutations being run out of sequence or multiple times, regardless of syntax.

2 Likes

Jupyter has global state which you can modify even without meaning to.
It looks like you transformed liste into ['a', 'b', 'c', 'd'] at some point by accident.
After that liste.append('d') would result in liste==['a', 'b', 'c', 'd', 'd'] and liste+['d']; liste would print ['a', 'b', 'c', 'd']

Personally I think Spyder is a better starting point for beginners. It’s harder to make this type of mistake there, and if you do run into trouble there is more of a guarantee that you can copy-paste your code in a way that reproduces your error.

1 Like

Here’s a video i’m running it step by step

Here’s a ss from my teacher’s notebook which i’m learning python

And this is my notebook.

If your teacher uses Jupyter there’s not much you can do about that. (Except complain :wink: , maybe they’ll do better next year.)

Jupyter notes the execution order at the left of the cells. Ideally you want to see a sequence like [33] [34] [35] [36] from top to bottom.
You have [24] [28] [32] [34].
That suggests something is messed up and you’re executing all your cells twice when they execute?

3 Likes

I agree with Peter. You are almost certainly executing the cells twice, and the problem is that the new value becomes the starting point for the second execution. One way to check what is happening is to put in one cell the following three statements.

print(liste)
liste = liste + ['e']
print(liste)

That way when you execute the cell you can see the value before and after execution.

I hope that helps.

I was using shift+enter to add a new cell and it’s messing up the cell order. Thank you!