+ 10

Why one change is adopted when another is not?

a = [1,2,[9,10,45]] b = a[:] b[1] = 7 b[2][2] = 7 print(a) output : [1,2,[9,10,7]] why did a adopt the second change but not the first one??

8th Apr 2020, 8:57 PM
M Tamim
M Tamim - avatar
1 Respuesta
+ 3
a = [1,2,[9,10,45]] print('a',a) b = a[:] print('b', b) b[1] = 7 print('b', b) b[2][2] = 7 print('b', b) a[1] = 7 print('a', a) #output : [1,2,[9,10,7]] #why did a adopt the second change but not the first one?? #try to run this you will have some idea. # the second changed the value of 'a' because in the second one we are replacing the element of another list which is nested in 'a', while on the otherhand in the first case we are trying to replace the value of 'a' using b i.e. b[1]=7 directly and not 'a' itself.
8th Apr 2020, 10:01 PM
RKK
RKK - avatar