Python # About reference and copy

Variables may not be the entity of a value

When dealing with specific types such as list and dictionary, variables refer to an entity, unlike a value entity. The nature of the variables can be confirmed from the execution results of the following programs.

l1 = []
l1.append(0)

l2 = l1
l2.append(1)

print('l1 = ' ,l1)
print('l2 = ' ,l2)

Execution result

l1 = [0, 1] l2 = [0, 1]

After substituting the list l1 into the list l2, I added a value only to l2, but a similar value was added to l1. This is because it is the reference information that holds both l1 and l2, and the entities pointed to by the reference share the same thing.

Copy the entity itself

The copy () method is used to solve the problem that the value itself is not copied just by making an assignment.

l1 = []
l1.append(0)

l3 = l1.copy()
l3.append(1)

print('l1 = ' ,l1)
print('l3 = ' ,l3)

Execution result

l1 = [0] l3 = [0, 1]

However, even if the copy () method is used, if the element of the list is a reference, the reference destination is not copied.

l4 = [[]]
l4[0].append(0)

l5 = l4.copy()
l5[0].append(1)

print('l4 = ' ,l4)
print('l5 = ' ,l5)

Execution result

l4 = [[0, 1]] l5 = [[0, 1]]

If you want to copy all the reference destinations of the elements included in the list, you can use the deepcopy () method included in the copy package.

import copy

l4 = [[]]
l4[0].append(0)

l6 = copy.deepcopy(l4)
l6[0].append(1)

print('l4 = ' ,l4)
print('l6 = ' ,l6)

Execution result

l4 = [[0]] l6 = [[0, 1]]

Recommended Posts

Python # About reference and copy
About python reference
About python objects and classes
About Python variables and objects
Python shallow copy and deep copy
Python shallow and deep copy
About Python, len () and randint ()
About Python and regular expressions
About Python and os operations
About Python sort () and reverse ()
About _ and __
About python dict and sorted functions
About dtypes in Python and Cython
About Python pickle (cPickle) and marshal
[Python] About Executor and Future classes
About Python, from and import, as
Reputation of Python books and reference books
About python slices
About python comprehension
About reference type
About Python tqdm.
About python, class
Copy file and rewrite cell value @python
About python inheritance
About python, range ()
Copy of python
A story about Python pop and append
About Python decorators
Python reference page
[Python] About multi-process
Talking about old and new Python classes
Talking about Python class attributes and metaclasses
About the behavior of copy, deepcopy and numpy.copy
Think about depth-priority and width-priority searches in Python
About the difference between "==" and "is" in python
A story about modifying Python and adding functions
About the * (asterisk) argument of python (and itertools.starmap)
[python] Compress and decompress
About Python for loops
About Class and Instance
Summary about Python scraping
About function arguments (python)
Python and numpy tips
[Python] pip and wheel
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
About cumprod and cummax
About Python, for ~ (range)
About Python3 character code
python input and output
[Python] Memo about errors
Python and Ruby split
About Python development environment
Python: About function arguments
Python, about exception handling
About Python3 ... (Ellipsis object)
[Python] About standard input