0

I am new to python, can some one help me understand this? Thank you Here we are only modifying the [0][0] value to X, why all values are changing?

board=[raw]*3
board[0][0] ='X'
print(board)
[['X', '', ''], ['X', '', ''], ['X', '', '']]

Can you help me understand why the b and d doesn't have the same output?

a=[1,2,3,4]
b=a
a=a+[5,6,7,8]
c=[1,2,3,4]
d=c
c+=[5,6,7,8]
print(a,b,c,d)
[1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4] [1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7, 8]

For the below one, as we are referring b=a, even after changing the values, will it be same?

c =['a','b','c']

a=[1,2,3,c]

b=a

c[0]='x'
a[0]='y'
print(a,b,c)
['y', 2, 3, ['x', 'b', 'c']] ['y', 2, 3, ['x', 'b', 'c']] ['x', 'b', 'c']
Rahul
  • 243
  • 2
  • 6
  • 17
  • 2
    `[raw]*3` creates three references to the one `raw` list. It doesn't create copies. Similarly, `b=a` makes `b` and `a` refer to the same list; that also doesn't make a copy. – Carcigenicate Sep 13 '22 at 00:08

0 Answers0