Why does not the + operator change a list while .append() does?

append, concatenation, list, python

Solution

So in a function the adding a 6 to the set doesn't show but it does when not in a function?

No, that is not what happens.

What happens is that, when you execute `mylist = mylist + [6]`, you are effectively creating an entirely new list and putting it in the local `mylist` variable. This `mylist` variable will vanish after the execution of the function and the newly created list will vanish as well.

OTOH when you execute `mylist.append(6)` you do not create a new list. You get the list already in the `mylist` variable and add a new element to this same list. The result is that the list (which is pointed by `list2` too) will be altered itself. The `mylist` variable will vanish again, but in tis case you altered the original list.

Let us see if a more visual explanation can help you :)

What happens when you call `proc()`

When you write `list1 = [1, 2, 3, 4, 5]` you are creating a new list object (at the right side of the equals sign) and creating a new variable, `list1`, which will point to this object.

Then, when you call `proc()`, you create another new variable, `mylist`, and since you pass `list1` as parameter, `mylist` will point to the same object:

However, the operation `mylist + [6]` creates a whole new list object whose contents are the contents of the object pointed by `mylist` plus the content of the following list object - that is, `[6]`. Since you attribute this new object to `mylist`, our scenario changes a bit and `mylist` does not point to the same object pointed by `list1` anymore:

What I have not said is that `mylist` is a local variable: it will disappear after the end of the `proc()` function. So, when the `proc()` execution ended, the `mylist` is gone:

Since no other variable points to the object generated by `mylist + [6]`, it will disappear, too (since the garbage collector* will collect it):

Note that, in the end, the object pointed by `list1` is not changed.

What happens when you call `proc2()`

Everything changes when you call `proc2()`. At first, it is the same thing: you create a list...

...and pass it as a parameter to a function, which will generate a local variable:

However, instead of using the `+` concatenation operator, which generates a new list, you apply the `append()` method to the existing list. The `append()` method does not create a new object; instead, it _changes the existing one:

After the end of the function, the local variable will disappear, but the original object pointed by it and by `list1` will be already altered:

Since it is still pointed by `list1`, the original list is not destroyed.

EDIT: if you want to take a look at all this stuff happening before your eyes just go to this radically amazing simulator:

* If you do not know what is garbage collector... well, you will discover soon after understanding your own question.

Problem

I'm working through Udacity and Dave Evans introduced an exercise about list properties ``` list1 = [1,2,3,4] list2 = [1,2,3,4] list1=list1+[6] print(list1) list2.append(6) print(list2) list1 = [1,2,3,4] list2 = [1,2,3,4] def proc(mylist): mylist = mylist + [6] def proc2(mylist): mylist.append(6) # Can you explain the results given by the four print statements below? Remove # the hashes # and run the code to check. print (list1) proc(list1) print (list1) print (list2) proc2(list2) print (list2) ``` The output is ``` [1, 2, 3, 4, 6] [1, 2, 3, 4, 6] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4, 6] ``` So in a function the adding a 6 to the set doesn't show but it does when not in a function?

Original source

Related problems