Python shallow copy and deep copy

I didn't understand it at all, so make a note

Even with shallow copy, the value of the latest element (?) Is copied.

>>> import copy
>>> a = {1:[1,2,3],2:0}
>>> b = copy.copy(a)
>>> c = copy.deepcopy(a)
>>> a
{1: [1, 2, 3], 2: 0}
>>> b
{1: [1, 2, 3], 2: 0}
>>> c
{1: [1, 2, 3], 2: 0}
>>> a[1].append(100)
>>> a[2] = 200
>>> a
{1: [1, 2, 3, 100], 2: 200}
>>> b
{1: [1, 2, 3, 100], 2: 0}
>>> c
{1: [1, 2, 3], 2: 0}
>>>

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 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
Ruby, Python and map
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
Encryption and decryption with Python
Python: Class and instance variables
3-3, Python strings and character codes
Python 2 series and 3 series (Anaconda edition)
Python and hardware-Using RS232C with Python-
Python on Ruby and angry Ruby on Python
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