python list by value not by reference [duplicate] python list by value not by reference [duplicate] python python

python list by value not by reference [duplicate]


You cannot pass anything by value in Python. If you want to make a copy of a, you can do so explicitly, as described in the official Python FAQ:

b = a[:]


To copy a list you can use list(a) or a[:]. In both cases a new object is created.
These two methods, however, have limitations with collections of mutable objects as inner objects keep their references intact:

>>> a = [[1,2],[3],[4]]>>> b = a[:]>>> c = list(a)>>> c[0].append(9)>>> a[[1, 2, 9], [3], [4]]>>> c[[1, 2, 9], [3], [4]]>>> b[[1, 2, 9], [3], [4]]>>> 

If you want a full copy of your objects you need copy.deepcopy

>>> from copy import deepcopy>>> a = [[1,2],[3],[4]]>>> b = a[:]>>> c = deepcopy(a)>>> c[0].append(9)>>> a[[1, 2], [3], [4]]>>> b[[1, 2], [3], [4]]>>> c[[1, 2, 9], [3], [4]]>>> 


In terms of performance my favorite answer would be:

b.extend(a)

Check how the related alternatives compare with each other in terms of performance:

In [1]: import timeitIn [2]: timeit.timeit('b.extend(a)', setup='b=[];a=range(0,10)', number=100000000)Out[2]: 9.623248100280762In [3]: timeit.timeit('b = a[:]', setup='b=[];a=range(0,10)', number=100000000)Out[3]: 10.84756088256836In [4]: timeit.timeit('b = list(a)', setup='b=[];a=range(0,10)', number=100000000)Out[4]: 21.46313500404358In [5]: timeit.timeit('b = [elem for elem in a]', setup='b=[];a=range(0,10)', number=100000000)Out[5]: 66.99795293807983In [6]: timeit.timeit('for elem in a: b.append(elem)', setup='b=[];a=range(0,10)', number=100000000)Out[6]: 67.9775960445404In [7]: timeit.timeit('b = deepcopy(a)', setup='from copy import deepcopy; b=[];a=range(0,10)', number=100000000)Out[7]: 1216.1108016967773