This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Adding number to a declared 2 dimension list
Type: behavior Stage: resolved
Components: Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, ezio.melotti, mrabarnett, void4buser
Priority: normal Keywords:

Created on 2020-11-23 22:06 by void4buser, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
bug.png void4buser, 2020-11-23 22:06 Screenshot about the bug
Messages (2)
msg381698 - (view) Author: void abuser (void4buser) Date: 2020-11-23 22:06
So basically the bug that I found was about adding a number to a sub-list.
If you declare li1 as li1=[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] in the code below then the code is working as it should do.
Although if you create the list called li1 with the same way how I did in the code below (which has the same outcome in terms of looking at that 2 dimension list) then the output is different and in fact false.
The code:
li1=[[0]*4]*4 for x in range(4): li1[0][1]+=1 print(li1)
Expected behavior
Expected output:
[[0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 2, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 3, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 4, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
The output that I got:
[[0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0]]
[[0, 2, 0, 0], [0, 2, 0, 0], [0, 2, 0, 0], [0, 2, 0, 0]]
[[0, 3, 0, 0], [0, 3, 0, 0], [0, 3, 0, 0], [0, 3, 0, 0]]
[[0, 4, 0, 0], [0, 4, 0, 0], [0, 4, 0, 0], [0, 4, 0, 0]]
msg381699 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-11-23 22:41
This isn't a bug. All of your nested lists are the same. See for example: https://stackoverflow.com/questions/44195396/initializing-an-m-x-n-array-of-0s-in-python
History
Date User Action Args
2022-04-11 14:59:38adminsetgithub: 86615
2020-11-23 22:41:45eric.smithsetstatus: open -> closed

components: - Regular Expressions

nosy: + eric.smith
messages: + msg381699
resolution: not a bug
stage: resolved
2020-11-23 22:06:39void4busercreate