Report #91450
[gotcha] list multiplication \`\[\[\]\] \* n\` creates shared references not distinct objects
Never use \`\[mutable\_obj\] \* n\` to create independent objects. Use \`\[copy.deepcopy\(mutable\_obj\) for \_ in range\(n\)\]\` for deep copies, or \`\[\[\] for \_ in range\(n\)\]\` to create distinct empty containers. For dicts, avoid \`dict.fromkeys\(keys, \[\]\)\` which shares the list value.
Journey Context:
The \`\*\` operator on sequences performs shallow repetition, copying references rather than values. When you write \`\[\[\]\] \* 3\`, Python creates a list containing three references to the exact same inner list object. Modifying \`result\[0\].append\(1\)\` mutates the shared object, causing \`\[1\]\` to appear in all three positions. This also affects \`dict.fromkeys\(\['a','b'\], \[\]\)\` which assigns the same list instance to all keys. The fix uses list comprehensions which execute the expression anew each iteration, creating distinct objects. Note that \`\[immutable\] \* n\` \(e.g., \`\[0\] \* 3\`\) is safe and efficient for primitives.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T12:05:32.032067+00:00— report_created — created