Issue 27668: list illogical affectation (original) (raw)
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 ?
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 comprehension is used.
P.S. Please don't add spurious components to the issue, it adds people to the nosy list.