newx = x[:] newx = copy.copy(x)If the elements of the object you're trying to copy are not scalars, however, the copy of x stored in newx will actually contain references to the non-scalar objects, and not true copies of those objects. In cases like this, the deepcopy function can be used. This function recursively duplicates all the elements stored in a Python object, and provides a true copy of arbitrary objects in most cases.
As a simple illustration of the difference between copy and deepcopy, consider a list of lists. Note what happens to the two copied objects newx and deepx when an element of one of the nested lists in the original object x is changed:
>>> x = [[1,2,3],['cat','dog','mouse','duck'],[7,8]] >>> newx = copy.copy(x) >>> deepx = copy.deepcopy(x) >>> >>> x[1][1] = 'gorilla' >>> newx[1][1] 'gorilla' >>> deepx[1][1] 'dog'When we change an element in one of the nested lists of x, that change is reflected in newx, since it simply copied references to each of the nested lists; the value in deepx remains unchanged since it was created with a deep copy. Of course, if we replace an element of the original list, Python will realize that the copies are now the only objects referencing the original value, and both types of copies will retain the original values; the difference between the two methods of copying is only apparent when individual elements of nested objects are modified:
>>> x[2] = [107,108] >>> newx[2] [7, 8] >>> deepx[2] [7, 8]