Consider:
a = [1, 2, 3]
When I slice it I get a new list b
:
b = a[:2]
If I now alter list b
:
b[0] = 0
I get a = [1, 2, 3]
and b = [0, 2]
. Why doesn't the a
list get altered as well? I know that slicing creates a new list object, but why is it not the object of the same elements as the inital object? How can I slice a
so that:
a = [1, 2, 3]
b = a[:2]
b[0] = 0
Results in a = [0, 2, 3]
and b = [0, 2]
.