Emanuel Barry added the comment:
To add to what SilentGhost just said; the '*' operator for lists doesn't create
new lists, but simply creates new references to the same list (i.e. it doesn't
re-evaluate the expression).
To create separate lists, you can do a list comprehension such as:
[['x'
SilentGhost added the comment:
Because elements of the tab list are the same list. And since list is a mutable
type, modifying list object will be visible to via all the reference / names
that point to that object. There are various ways to work around this
behaviour, typically a list comprehe
New submission from antoine Zellmeyer:
>>> tab = [['x']*3]*3
>>> tab
[['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']]
>>> tab[1][0] = 5
>>> tab
[[5, 'x', 'x'], [5, 'x', 'x'], [5, 'x', 'x']]
>>>
why not only the element tab[1][0] is changed ?
--
assignee: terry.reedy
components: IDLE