Python> list> 3 copy methods> .copy () / list () / [:]> Works on Python3 / Error with .copy () on Python2> All were shallow copy

@ 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.

Additional learning

As @shiracamus comment, all the above copies were shallow copies.

Recommended Posts

Python> list> 3 copy methods> .copy () / list () / [:]> Works on Python3 / Error with .copy () on Python2> All were shallow copy
Create a list in Python with all followers on twitter
Python error list (Japanese)
Initialize list with python
Python shallow copy and deep copy
Python shallow and deep copy
Error when playing with python
[Python] Copy of multidimensional list
Works with Python and R
Error and solution when installing python3 with homebrew on mac (catalina 10.15)
Convert list to DataFrame with python
Notes on using rstrip with python.
Getting started with Python 3.8 on Windows
return errdetails on error with grpc-node
[Memo] Tweet on twitter with python