[New-bugs-announce] [issue45169] Shallow copy occurs when list multiplication is used to create nested lists; can confuse users

2106 Hyunwoo Oh report at bugs.python.org
Fri Sep 10 22:55:07 EDT 2021


New submission from 2106 Hyunwoo Oh <ohwphil at gmail.com>:

If you do the following:

lists=[[]]*100
lists[1].append('text')
print(lists[2])

you can see lists[2] contains 'text' even though it was appended to lists[1] in the text. A little more investigation with the id() function can show that the lists are shallowly copied when list multiplication occurs. I think this can confuse users when they try to use list multiplication to create nested lists, as they expected a deep copy to occur.

----------
components: Demos and Tools
messages: 401625
nosy: ohwphil
priority: normal
severity: normal
status: open
title: Shallow copy occurs when list multiplication is used to create nested lists; can confuse users
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45169>
_______________________________________


More information about the New-bugs-announce mailing list