I'm addicted to Python 2D lists

At first

I'm still a beginner so if the explanation is wrong or if I should write more, please tell me.

Being addicted

There is a list that contains - as many as mx to make my.

mx,my = 3,3
l = [list('-'*mx)]*my

print(l)
# [['-','-','-'],['-','-','-'],['-','-','-']]

The result is as expected.

But suppose you want the first of the second list to be +. Then that! ??

l[1][0] = '+'

print(l)
# [['-','+','-'],['-','+','-'],['-','+','-']]

The list doesn't meet my expectations. I just wanted to do it on the second list, but it's all over.

Reason

If you check all the identification IDs with the id function

print(id(l[0]))
# 40116936
print(id(l[1]))
# 40116936
print(id(l[2]))
# 40116936

All the same ... No matter where you put it, the same ID will be entered because it has the same ID as the others. (The explanation of the reason may not be appropriate)

approach

I could do it by making new ones one by one with the for statement.

mx,my = 3,3
l = [['-' for x in range(mx)] for y in range(mx)]

If you put + in the first of the second list

l[1][0] = '+'

print(l)
# [['-','-','-'],['-','-','-'],['-','+','-']]

The result was as expected.

the end

the end

Recommended Posts

I'm addicted to Python 2D lists
[Python] How to convert a 2D list to a 1D list
What I was addicted to Python autorun
Updated to Python 2.7.9
I want to tweet on Twitter with Python, but I'm addicted to it
"Backport" to python 2
I'm addicted to Kintone as a data store
An article summarizing the pitfalls addicted to python
Python I'm also basic
Solve addition (equivalent to paiza rank D) in Python
Calculate 2D IDCT ~ Python
Changes from Python 3.0 to Python 3.5
Solve multiplication (equivalent to paiza rank D) in Python
[Python] Binary Acing 2020D
Memorize Python commentary 5 --Lists
Try to operate DB with Python and visualize with d3
Python3 | Lists, Tuples, Dictionaries
How to install python
python decorator to retry
Introduction to Python language
Things to keep in mind when copying Python lists
Python lists, tuples, dictionaries
I was addicted to scraping with Selenium (+ Python) in 2020
[Spark] I'm addicted to trapping "", null and [] in DataFrame
Note to daemonize python
Introducing Python 2.7 to CentOS 6.6
Connect python to mysql
[Python MinMaxScaler] Normalize to 0 ~ 1
[Python] How to create a 2D histogram with Matplotlib
Save lists, dictionaries and tuples to external files python
What I was addicted to when using Python tornado
Solve number sorting (equivalent to paiza rank D) in Python
[python] Summary of how to retrieve lists and dictionary elements
How to transpose a 2D array using only python [Note]
What I was addicted to when migrating Processing users to Python
Solve character matches (equivalent to paiza rank D) in Python
[Fixed] I was addicted to alphanumeric judgment of Python strings
Manipulating Python character lists (arrays)
Connect to BigQuery with Python
[2020.8 latest] How to install Python
[python] Convert date to string
Post from Python to Slack
How to install Python [Windows]
Post to vim → Python → Slack
Introduction to Python Django (2) Win
To flush stdout in Python
Convert numpy int64 to python int
python3: How to use bottle (2)
[Python] Convert list to Pandas [Pandas]
Cheating from PHP to Python
A road to intermediate Python
Try to understand Python self
Python notes to forget soon
[Python] How to use list 1
Create 3d gif with python3
Login to website in Python
Connect to Wikipedia with Python
How to update Python Tkinter to 8.6
Python: 3D array image (numpy.array)
[Python] Dynamic programming TDPC D
Post to slack with Python 3