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?
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 localmylistvariable. Thismylistvariable 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 themylistvariable and add a new element to this same list. The result is that the list (which is pointed bylist2too) will be altered itself. Themylistvariable 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 passlist1as parameter,mylistwill 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 bymylistplus the content of the following list object – that is,[6]. Since you attribute this new object tomylist, our scenario changes a bit andmylistdoes not point to the same object pointed bylist1anymore:What I have not said is that
mylistis a local variable: it will disappear after the end of theproc()function. So, when theproc()execution ended, themylistis 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
list1is 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 theappend()method to the existing list. Theappend()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
list1will 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.