Given that everything in python is by reference, I do understand what's happening in the code below:
a = ['one']*3 // a = ['one', 'one', 'one']
b = [a]*3 // b = [['one', 'one', 'one'], ['one', 'one', 'one'], ['one', 'one', 'one']]
b[1][2] = 'two'
And now, b is
[['one', 'one', 'two'], ['one', 'one', 'two'], ['one', 'one', 'two']]
Because we made b
refer three times the same object referred by a
, reassigning any one of the component, change is seen at three places.
But, then why the same thing occurs when
a = [['one']]*3 // a = [['one'], ['one'], ['one']]
a[1] = ['two']
does not make a = ['two', 'two', 'two']
, but just [['one'], ['two'], ['one']
as if a now has three different objects pointed to.
Am I missing some logic here?
Thanks in advance, Nikhil