While reading about slicing a list; i got stuck here:-
a = [1, 3, 5]
b = a[:]
a[:] = [x**2 for x in a]
a[:] = [0]
print(b) # output --> [1,3,5]
And this:-
a = [1, 3, 5]
b = a
a[:] = [x**2 for x in a]
a[:] = [0]
print(b) # output --> [0]
I know that b = a[:]
is making a copy of list a
but then what b=a
is doing in the second example? And when printing the outputs, in the first case b
doesn't get modified but get modified in second one. What is the reason for this behavior?
I am not asking about how to do slicing, but wondering why both the codes mentioned are behaving strangely and differently.