Report #17311
[gotcha] \[\[0\] \* 3\] \* 3 or \[\[0\]\] \* 3 creates multiple references to the same inner list, causing mutation aliasing
Use list comprehensions to create independent inner objects: \[\[0 for \_ in range\(3\)\] for \_ in range\(3\)\] or \[0\] \* 3 for a flat list of immutables only.
Journey Context:
The \* operator on lists repeats the reference to the object, not creating deep copies. When the repeated object is immutable \(int, string, tuple\), this is fine. When it's mutable \(list, dict, set\), all indices in the outer list point to the same memory location. Modifying one inner list appears to "modify" all others simultaneously. This is a classic gotcha for people coming from MATLAB or R where matrix initialization syntax often works differently. The comprehension syntax forces re-evaluation of the inner expression for each iteration, creating distinct objects. For multi-dimensional structures, consider numpy arrays which handle this correctly via broadcasting and copying semantics.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T04:57:44.060999+00:00— report_created — created