copy.copy() vs copy.deepcopy()
copy.copy() and copy.deepcopy() are both Python Copy Module APIs, and choosing between them is a common point of confusion. This page compares their syntax, parameters, return values, and typical use cases so you can pick the right one. copy.copy(): Returns a shallow copy of object x. Nested objects are not copied — they are referenced. Its syntax is copy.copy(x), and it returns shallow copy copy.deepcopy(): Returns a deep copy of object x (recursively copies all nested objects). Its syntax is copy.deepcopy(x, memo=None, _nil=[]), and it returns deep copy Example of copy.copy(): import copy
orig = [1, [2, 3], 4]
copied = copy.copy(orig)
copied[1].append(99) # Also affects orig! (shallow). Example of copy.deepcopy(): import copy
orig = [1, [2, 3], 4]
cloned = copy.deepcopy(orig)
cloned[1].append(99) # Does NOT affect orig. Read the full references for copy.copy() and copy.deepcopy() to see parameters, edge cases, and example code side by side. Both belong to the Copy Module category in the Python Reference documentation.