Things to keep in mind when copying Python lists

Introduction

Note that you should be careful when copying lists in Python. For more details, please search for "pass by value / pass by reference".

Be careful

Look at the code below before explaining it in words.

X = 2
Y = X
Y = 5
print('Y = ', Y)
print('X = ', X)

If you do this, of course

Y =  5
X =  2

The result is obtained. Next, try running the following code.

i = [1, 2, 3, 4, 5]
j = i
j[0] = 100
print('j = ', j)
print('i = ', i)

Here, a list called i is decided, i is assigned to j, the value of index number 0 of j is set to 100, and i and j are output respectively. The result is

j =  [100, 2, 3, 4, 5]
i =  [100, 2, 3, 4, 5]

And i has also changed. You can see the cause of this in the following code.

print(id(X))
print(id(Y))
print(id(i))
print(id(j))
4436251808
4436251904
4304286176
4304286176

It turns out that X and Y with the assigned values have different ids, while i and j with the assigned list have the same id. Therefore, in the list, the changes made in j will be reflected in i as well. Therefore, when copying the list,

j = i.copy()

Finally

I've heard that you rarely copy lists, so I don't think you'll use them very often, but be careful not to lead to bugs.

Recommended Posts

Things to keep in mind when copying Python lists
Things to keep in mind when developing crawlers in Python
Things to keep in mind when processing strings in Python2
Things to keep in mind when processing strings in Python3
Things to keep in mind when using Python with AtCoder
Things to keep in mind when using cgi with python.
Things to keep in mind when using Python for those who use MATLAB
Things to keep in mind when building automation tools for the manufacturing floor in Python
Things to keep in mind when converting row vectors to column vectors with ndarray
Things to note when initializing a list in Python
Things to keep in mind when doing Batch Prediction on GCP ML Engine
Things to watch out for when using default arguments in Python
Summary of points to keep in mind when writing a program that runs on Python 2.5
Error when trying to install psycopg2 in Python
How to exit when using Python in Terminal (Mac)
I want to do something in Python when I finish
To flush stdout in Python
Login to website in Python
Attention when os.mkdir in Python
Speech to speech in python [text to speech]
How to develop in Python
Post to Slack in Python
Convenient writing method when appending to list continuously in Python
What to do when "SSL: CERTIFICATE_VERIFY_FAILED _ssl.c: 1056" appears in Python
[Subprocess] When you want to execute another Python program in Python code
How to not escape Japanese when dealing with json in python
Precautions when using pit in Python
Things to watch out for when naming dynamic routing in nuxt.js
Convert markdown to PDF in Python
How to collect images in Python
How to use SQLite in Python
In the python command python points to python3.8
Timezone specification when converting a string to datetime type in python
[Python] When you want to use all variables in another file
Try to calculate Trace in Python
Precautions when passing def to sorted and groupby functions in Python? ??
Atom: Note for Indentation Error when copying Python script to shell
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
I'm addicted to Python 2D lists
Articles to read when Blender Python script code doesn't work in 2.80
What to do when ModuleNotFoundError: No module named'XXX' occurs in Python
Precautions when giving default values to arguments in Python function definitions
How to handle Japanese in Python
An alternative to `pause` in Python
What to do when the value type is ambiguous in Python?
When using regular expressions in Python
When writing a program in Python
Things to watch out for when creating a Python environment on a Mac
How to hide the command prompt when running python in visual studio 2015
How to write a string when there are multiple lines in python
I tried to implement PLSA in Python
[Introduction to Python] How to use class in Python?
Try logging in to qiita with Python
Install Pyaudio to play wave in python
I tried to implement permutation in Python
Method to build Python environment in Xcode 6
How to dynamically define variables in Python