KITFORMA · EDITORIAL ANSWER
Why do all rows of a Python matrix change when I edit one cell?
KitForma editorial guide. The expression `[[0] * width] * height` repeats references to one inner list. It does not create independent rows.
Step-by-step answer
[[0] * width] * height repeats references to one inner list. It does not create independent rows.
Construct each row separately with [[0] * width for _ in range(height)]. Change one cell and assert that cells in the same column of other rows remain unchanged. This pattern differs from a shallow copy of an existing matrix: both involve aliasing, but the repeated-row bug begins at construction. If each cell itself must be a mutable object, construct each cell separately too. Repeating an immutable zero is safe; repeating a mutable list or dictionary is not independent allocation.Sources and verification
Sources checked:
Scope: This editorial guide is based on the cited sources and tool behavior. A forum question or a query observed for our site does not establish market search volume, low competition, guaranteed rankings or inadequate answers elsewhere.
This editorial answer was prepared by KitForma with AI assistance. It is not presented as a real member question or an independent user review. Check the sources and the result with your own file; report corrections in the discussion.