I’m quite confused about the use of methods to manipulate a list in python. Say you have
mylist = [1,2,3,4]
mylist.append(5) #works fine,
but when I put
def adding(mylist):
mylist.append(5)
print adding(mylist) #will print out the orginal list without the 5.
Also,
data2 = data3 = [1,2,3]
data2 = data2 +[4]+[5]+[6] # doesn't works
data3 += [4] +[5] +[6] #works
But I’m quite confused how they work.
So I want to make a list(not in terms of python) of different ways to manipulate a list. Can anyone help?
I suspect the reason you expect the original code to work is because you are
more familiar with passing by references in languages like C++. It is important
then to be aware of the difference of pass by reference and pass by value.
It is even more important to understand which one Python is using.
In short, Python actually uses neither pass by reference or pass by value;
passing is done via object in Python.
Long answer:
An example of pass by value can be made with C:
Here, the value of i is copied when passed to
adding(). theint iinsideadding(int i)resides in a different memory location than the originalintinsidei
main(). So insideadding(),i = i + 1only affects the value inthe chunk of memory known only inside the scope of
adding(), theiinmain()is totally unaffected because it resides in a different chunk ofmemory.
An example of pass by reference can be made with C++:
Here
iis passed by reference toadding(). Theiinsideadding()refersto exactly the same chunk of memory as the
iinmain(). And hence,iwillbe incremented to 1.
Now, let’s talk about Python by going back to your example (that I modified
by guessing what you actually meant =]):
Here,
mylistis passed neither by value or reference. It is in fact passed byobject.
In Python,
mylist = [1,2,3,4]creates a list[1,2,3,4]and attach atag
mylistto it. The important thing here to note is that list is anobject (IIRC, every object is a
PyObjector an extension to it).Understand this distinction will enable you to make sense of the outputs in the
example cases:
In Case 1,
.append()is called on[1,2,3,4]list object directly toalter the list value, and hence the object
myobjectis attaching to haschanged. Notice that this is possible also because lists are mutable objects in
Python.
In Case 2,
mylistis NOT copied on passing toadding(), it is still“attached” to the same list object, similar to Case 1. Hence, calling
.append()on this object will alter the object itself. Outside ofadding(), sincemyliststill attaches to the same object (even though theobject has altered itself), we get the output [1,2,3,4,5].
In Case 3, inside
adding(), the local variablemylistis attached toa new object created by
[1,2,3,4]+[4]. But the object the originalmylistis pointing to has never been altered, therefore, the output remains to be
[1,2,3,4].Case 4 is a bit tricky. In Python lists are mutable,
+=actsas a shortcut like
.extend(), which acts on the calling object itself. Hence,the original object has been modified to
[1,2,3,4,5]andmyliststillattaches to it. It is perhaps also worth noting that for immutable objects like
tuples, strings and numbers,
+=will create a new object and attach thevariable to it.
As for different ways to use lists in Python, this article written by Fredrik Lundh contains very thorough information.