@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic (No. 1424 / 12833)
There seem to be three ways to copy the list.
I've tried.
a = [ 'I am clone ' ]
b = a.copy()
c = list(a)
d = a[:]
a = [ 'I am original']
print(a)
print(b)
print(c)
print(d)
Python 3
http://ideone.com/Pmchv0
result
['I am original']
['I am clone ']
['I am clone ']
['I am clone ']
Python 2
http://ideone.com/l5flEA
result
Traceback (most recent call last):
File "prog.py", line 2, in <module>
AttributeError: 'list' object has no attribute 'copy'
docs
Python 3 https://docs.python.org/3.6/tutorial/datastructures.html
Python 2 https://docs.python.org/2/tutorial/datastructures.html
Python 3 has a description of list.copy (), Python 2 does not.
As @shiracamus comment, all the above copies were shallow copies.
Recommended Posts