Python shallow and deep copy

I recently searched for shallow and deep copies of the list, so make a note. (Reference: 8.10. Copy — Shallow copy and deep copy operations)

Normally, if you copy a list and change the value of one list, the value of the other list will also change ~~ (shallow copy) ~~

2017/9/17 postscript I received a comment. Thank you dlpyvim.

I don't think the example above is a shallow copy. A shallow copy is copy.copy (). The difference from copy.deepcopy () is whether to insert a reference or copy recursively if the object is inside an object.

>>> l = [1, 2, 3]
>>> l2 = l
>>> l
[1, 2, 3]
>>> l2
[1, 2, 3]
>>> l[2] = 4
>>> l
[1, 2, 4]
>>> l2
[1, 2, 4]

To avoid this, it seems that you can use the deepcopy () function (deep copy).

>>> l = [1, 2, 3]
>>> import copy
>>> l2 = copy.deepcopy(l)
>>> l
[1, 2, 3]
>>> l2
[1, 2, 3]
>>> l[2] = 4
>>> l
[1, 2, 4]
>>> l2
[1, 2, 3]

If you don't pay attention to this when you make an empty list, you will be in trouble because the value will be entered without permission.

>>> empty = [[]] * 5
>>> empty
[[], [], [], [], []]
>>> empty[0].append(1)
>>> empty
[[1], [1], [1], [1], [1]]

If you want to create such a two-dimensional empty list, you should use list comprehension notation.

>>> empty = [[] for x in range(5)]
>>> empty
[[], [], [], [], []]
>>> empty[0].append(1)
>>> empty
[[1], [], [], [], []]

Recommended Posts

Python shallow copy and deep copy
Python shallow and deep copy
python shallow / deep copy at a glance @ PythonTutor.org
About shallow and deep copies of Python / Ruby
Python # About reference and copy
"Deep copy" and "Shallow copy" to understand with the smallest example
Copy file and rewrite cell value @python
Copy of python
Python Deep Learning
Deep learning × Python
[python] Compress and decompress
Python and numpy tips
[Python] pip and wheel
Python: Deep Learning Practices
Batch design and python
Copy of python preferences
Python iterators and generators
Python packages and modules
Vue-Cli and Python integration
python input and output
Python and Ruby split
Python: Deep Learning Tuning
Python3, venv and Ansible
Python asyncio and ContextVar
Deep Python learned from DEAP
Programming with Python and Tkinter
Python: Class and instance variables
Python 2 series and 3 series (Anaconda edition)
Python and hardware-Using RS232C with Python-
Python indentation and string format
Python real division (/) and integer division (//)
Install Python and Flask (Windows 10)
About python objects and classes
About Python variables and objects
Apache mod_auth_tkt and Python AuthTkt
Å (Ongustromu) and NFC @ Python
[Python] Isn't it copied even though "copy ()" is done? Beliefs and failures about deep copying
Understand Python packages and modules
# 2 [python3] Separation and comment out
Python and ruby slice memo
Python installation and basic grammar
I compared Java and Python!
About Python, len () and randint ()
About Python datetime and timezone
Install Python 3.7 and Django 3.0 (CentOS)
Python environment construction and TensorFlow
Python class variables and instance variables
Ruby and Python syntax ~ branch ~
[Python] Python and security-① What is Python?
Stack and Queue in Python
python metaclass and sqlalchemy declareative
Fibonacci and prime implementations (python)
Python basics: conditions and iterations
Python bitwise operator and OR
Python debug and test module
[Python] Copy of multidimensional list
Python list and tuples and commas
Python variables and object IDs
Python list comprehensions and generators
About Python and regular expressions
python with pyenv and venv