By “shallow copying” it means the content of the dictionary is not copied by value, but just creating a new reference.
>>> a = {1: [1,2,3]} >>> b = a.copy() >>> a, b ({1: [1, 2, 3]}, {1: [1, 2, 3]}) >>> a[1].append(4) >>> a, b ({1: [1, 2, 3, 4]}, {1: [1, 2, 3, 4]})In contrast, a deep copy will copy all contents by value.
>>> import copy >>> c = copy.deepcopy(a) >>> a, c ({1: [1, 2, 3, 4]}, {1: [1, 2, 3, 4]}) >>> a[1].append(5) >>> a, c ({1: [1, 2, 3, 4, 5]}, {1: [1, 2, 3, 4]})So:
b = a: Reference assignment, Makeaandbpoints to the same object.
b = a.copy(): Shallow copying,aandbwill become two isolated objects, but their contents still share the same reference
b = copy.deepcopy(a): Deep copying,aandb‘s structure and content become completely isolated.
Source: python – Understanding dict.copy() – shallow or deep? – Stack Overflow
python – Understanding dict.copy() – shallow or deep? – Stack Overflow
![Illustration of 'a = b': 'a' and 'b' both point to '{1: L}', 'L' points to '[1, 2, 3]'.](https://i0.wp.com/rosetta.vn/short/wp-content/uploads/sites/3/2019/05/4AQC6.png?w=750&ssl=1)
![Illustration of 'b = a.copy()': 'a' points to '{1: L}', 'b' points to '{1: M}', 'L' and 'M' both point to '[1, 2, 3]'.](https://i0.wp.com/rosetta.vn/short/wp-content/uploads/sites/3/2019/05/Vtk4m.png?w=750&ssl=1)
![Illustration of 'b = copy.deepcopy(a)': 'a' points to '{1: L}', 'L' points to '[1, 2, 3]'; 'b' points to '{1: M}', 'M' points to a different instance of '[1, 2, 3]'.](https://i0.wp.com/rosetta.vn/short/wp-content/uploads/sites/3/2019/05/BO4qO.png?w=750&ssl=1)