[Python] Copy of multidimensional list

About the copy of the multidimensional list of Python, the behavior is difficult for me to understand, so I organized it with the experimental results. In conclusion, copy (), list [:], and copy.copy () in the copy library are shallow copies, creating a new multidimensional list and then the original. Insert a reference to the list found in the list. Copy.deepcopy () in the copy library is a deep copy, which creates a new multidimensional list and then inserts a copy of the list found in the original list.

copy() I'm not sure if the "first layer" is better or the "first dimension" is better ... The ID is naturally different because the copy creates another multidimensional list. However, since the contents of the list are references, rewriting the first layer of the copy does not affect the original list, but rewriting the second and subsequent layers spreads to the original even though the ID is different.

copy()


lst = [[0]*3 for _ in range(3)]

lst1 = lst.copy()

lst1[1] = ['r'] * 3
print('lst1 = ', lst1)
print('id(lst1) = %d' % id(lst1))
print('lst = ', lst)
print('id(lst) = %d' % id(lst))

lst1[0][1] = 'r'
print('lst1 = ', lst1)
print('id(lst1) = %d' % id(lst1))
print('lst = ', lst)
print('id(lst) = %d' % id(lst))

output


lst1 =  [[0, 0, 0], ['r', 'r', 'r'], [0, 0, 0]]
id(lst1) = 139730947400512
lst =  [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
id(lst) = 139730946890944
lst1 =  [[0, 'r', 0], ['r', 'r', 'r'], [0, 0, 0]]
id(lst1) = 139730947400512
lst =  [[0, 'r', 0], [0, 0, 0], [0, 0, 0]]
id(lst) = 139730946890944

List [:]

Same as copy ().

python:list[:]


lst = [[0]*3 for _ in range(3)]

lst1 = lst[:]

lst1[1] = ['r'] * 3
print('lst1 = ', lst1)
print('id(lst1) = %d' % id(lst1))
print('lst = ', lst)
print('id(lst) = %d' % id(lst))

lst1[0][1] = 'r'
print('lst1 = ', lst1)
print('id(lst1) = %d' % id(lst1))
print('lst = ', lst)
print('id(lst) = %d' % id(lst))

output


lst1 =  [[0, 0, 0], ['r', 'r', 'r'], [0, 0, 0]]
id(lst1) = 140313735131456
lst =  [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
id(lst) = 140313734622336
lst1 =  [[0, 'r', 0], ['r', 'r', 'r'], [0, 0, 0]]
id(lst1) = 140313735131456
lst =  [[0, 'r', 0], [0, 0, 0], [0, 0, 0]]
id(lst) = 140313734622336

copy library copy.copy ()

Same as copy ().

copy.copy()


import copy

lst = [[0]*3 for _ in range(3)]

lst1 = copy.copy(lst)

lst1[1] = ['r'] * 3
print('lst1 = ', lst1)
print('id(lst1) = %d' % id(lst1))
print('lst = ', lst)
print('id(lst) = %d' % id(lst))

lst1[0][1] = 'r'
print('lst1 = ', lst1)
print('id(lst1) = %d' % id(lst1))
print('lst = ', lst)
print('id(lst) = %d' % id(lst))

output


lst1 =  [[0, 0, 0], ['r', 'r', 'r'], [0, 0, 0]]
id(lst1) = 139783910001664
lst =  [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
id(lst) = 139783910057280
lst1 =  [[0, 'r', 0], ['r', 'r', 'r'], [0, 0, 0]]
id(lst1) = 139783910001664
lst =  [[0, 'r', 0], [0, 0, 0], [0, 0, 0]]
id(lst) = 139783910057280

copy library copy.deepcopy ()

Since copying creates another multidimensional list, the ID will be different and the contents of that list will also be copied. So to speak, a clone is made. The content is exactly the same, but different, so rewriting it will not affect the original.

copy.deepcopy()


import copy

lst = [[0]*3 for _ in range(3)]

lst1 = copy.deepcopy(lst)

lst1[1] = ['r'] * 3
print('lst1 = ', lst1)
print('id(lst1) = %d' % id(lst1))
print('lst = ', lst)
print('id(lst) = %d' % id(lst))

lst1[0][1] = 'r'
print('lst1 = ', lst1)
print('id(lst1) = %d' % id(lst1))
print('lst = ', lst)
print('id(lst) = %d' % id(lst))

output


lst1 =  [[0, 0, 0], ['r', 'r', 'r'], [0, 0, 0]]
id(lst1) = 139629482679488
lst =  [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
id(lst) = 139629482735040
lst1 =  [[0, 'r', 0], ['r', 'r', 'r'], [0, 0, 0]]
id(lst1) = 139629482679488
lst =  [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
id(lst) = 139629482735040

reference

Python --Duplicate list copy --- shallow copy and deep copy operations [Python] How to prevent the original list from being modified by assigning a list to the argument of a function you created

Recommended Posts

[Python] Copy of multidimensional list
Copy of python
List of python modules
Copy of multiple List
Copy of python preferences
[Introduction to Udemy Python 3 + Application] 19. Copy of list
Make a copy of the list in Python
Summary of Python3 list operations
Multidimensional array initialization of list
[Python] list
About the basics list of Python basics
Python basics: list
[Python] Chapter 04-03 Various data structures (multidimensional list)
Python multidimensional array
Basics of Python ①
Basics of python ①
[python] Get a list of instance variables
Basic grammar of Python3 series (list, tuple)
Summary of built-in methods in Python list
Summary of how to use Python list
[Python] Get a list of folders only
Python list manipulation
Introduction of Python
[Python / DynamoDB / boto3] List of operations I tried
[Introduction to Udemy Python 3 + Application] 26. Copy of dictionary
[python] Check the elements of the list all, any
[Python] Sort the list of pathlib.Path in natural sort
[python] Create a list of various character types
Basic operation list of Python3 list, tuple, dictionary, set
List of Python code to move and remember
[Python] Operation of enumerate
Python> list> extend () or + =
Unification of Python environment
Basics of Python scraping basics
Python list comprehension speed
Filter List in Python
[python] behavior of argmax
python unittest assertXXX list
Usage of Python locals ()
the zen of Python
List of activation functions (2020)
Installation of Python 3.3 rc1
[Memo] Python3 list sort
OpenCV3 Python API list
Python error list (Japanese)
List find in Python
# 4 [python] Basics of functions
Depth of nested list
Basic knowledge of Python
Sober trivia of python3
Summary of Python arguments
Display of fractions (list)
Basics of python: Output
Installation of matplotlib (Python 3.3.2)
Application of Python 3 vars
Python exception class list
Various processing of Python
Initialize list with python
Mayungo's Python Learning Note: List of stories and links
Make a copy of a Google Drive file from Python
Summary of Python sort (list, dictionary type, Series, DataFrame)