[issue25681] Assignment of one element in nested list changes multiple elements
Emanuel Barry
report at bugs.python.org
Fri Nov 20 09:48:06 EST 2015
Emanuel Barry added the comment:
Another way to fix this would be the following:
>>> l=[[''] * 2 for _ in range(3)]
>>> l
[['', ''], ['', ''], ['', '']]
>>> l[0][1] = "A"
>>> l
[['', 'A'], ['', ''], ['', '']]
The * operator on lists doesn't create new elements, it only adds new references to them. A list comprehension will evaluate the expression each time, while repetition (the * operator) will only evaluate it once.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25681>
_______________________________________
More information about the Python-bugs-list
mailing list