You asked two questions:
Deep vs. shallow copy
matrix[:]is a shallow copy -- it only copies the elements directly
stored in it, and doesn’t recursively duplicate the elements of arrays or
other references within itself. That means:
a = [[4]]b = a[:]a[0].append(5)print b[0] # Outputs [4, 5], as a[0] and b[0] point to the same array
The same would happen if you stored an object in
a.
deepcopy()is, naturally, a deep copy -- it makes copies of each of its
elements recursively, all the way down the tree:
a = [[4]]c = copy.deepcopy(a)a[0].append(5)print c[0] # Outputs [4], as c[0] is a copy of the elements of a[0] into a new array
Returning
return final.append(li)is different from calling
appendand returning
finalbecause list.append does not return the list object itself, it returns
None


![Python: deepcopy(list) vs new_list = old_list[:] Python: deepcopy(list) vs new_list = old_list[:]](http://www.mshxw.com/aiimages/31/661826.png)
