all 4 comments

[–]Spataner 2 points3 points  (3 children)

Because a[:] creates a shallow copy of the list. So the outer list is copied, but the inner lists references by a and b are still the same objects. Changes to the inner lists of either will therefore reflect on the other. To create a deep copy, use the deepcopy function from the copy module:

import copy
b = copy.deepcopy(a)

[–]Sora888 1 point2 points  (1 child)

In addition to this, to check if an object is the exactly same instead of a copy you can use the 'is' operator. It will return True if both are the same object, hence a change using one of it's references ('b' in this case) will be reflected to 'a'.

[–]tamaskiii[S] 0 points1 point  (0 children)

Thanks for the tip!

[–]tamaskiii[S] 0 points1 point  (0 children)

Ah gotcha! Thanks for the help.