Reputation: 189686
simple Python question:
Example list: A = [1,2,3,4,5]
I need to generate another list B which is a shallow copy of list A such that B is a new list containing the same elements in the same order (so that I can substitute one of B's elements w/o affecting A). How can I do this?
clarification: I want to do something like
def some_func(A)
B = {what do I do here to get a copy of A's elements?}
B[0] = some_other_func(B[0])
yet_another_func(B)
based on all your answers + the Python docs, a better way to do what I want is the following:
def some_func(A)
B = [some_other_func(A[0])] + A[1:]
yet_another_func(B)
thanks for pointing me in the right direction!
Upvotes: 2
Views: 286
Reputation: 12814
That would be a deep copy, not a shallow one.
Lists copy shallow by default. That's why there's a deepcopy
command in the copy module.
B = copy.deepcopy(A)
Optionally, B = A[:]
will do. But keep deepcopy in mind for future. More complex data types can benefit from it.
Added Info about copy:
A shallow copy:
b = [1,2]
a = b
b[0] = 11
print a // [1,11]
A deep copy:
b = [1,2]
a = b[:]
b[0] = 11
print a // [1,2]
But, furthermore:
>>> a = [[1,2]]
>>> b = a[:]
>>> b
[[1, 2]]
>>> a
[[1, 2]]
>>> a[0][0] = 11
>>> a
[[11, 2]]
>>> b
[[11, 2]]
>>>
So, the elements themselves are shallow copies in this case.
Upvotes: 8
Reputation: 879591
B=A[:]
suffices:
In [22]: A=[1,2]
In [23]: B=A[:]
In [24]: B[0]=100
In [25]: A
Out[25]: [1, 2]
In [26]: B
Out[26]: [100, 2]
A[:]
uses slice notation to get the slice with all the elements of A
.
Since slices of Python lists always return new lists, you get a copy of A
.
Note that the elements inside B are identical to the elements inside A. If the elements are mutable, mutating them through B will affect A.
Upvotes: 3
Reputation: 45542
Here are 3 ways to make a copy of list A:
Use slice notation:
copy_of_A = A[:]
Use the list constructor:
copy_of_A = list(A)
Use the copy module:
from copy import copy
copy_of_A = copy(A)
As you requested these copies are all shallow copies. To learn about the difference between shallow copy and deep copy read the documentation of the copy module.
Upvotes: 5
Reputation: 25349
You can perform that copy in the following way:
B = A[:]
Upvotes: 3