User Tools

Site Tools


python:shallowdeepcopy

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
python:shallowdeepcopy [2016/01/25 08:48]
admin
python:shallowdeepcopy [2016/01/25 09:08] (current)
admin
Line 1: Line 1:
 ====== Shallow and Deep Copy explained on lists ====== ====== Shallow and Deep Copy explained on lists ======
 25.01.2016 (Python 3.4) 25.01.2016 (Python 3.4)
- 
  
 The type of copying becomes relevant when dealing with compound objects (e.g. lists, sublists, class instances). The type of copying becomes relevant when dealing with compound objects (e.g. lists, sublists, class instances).
Line 27: Line 26:
 ===== Changing elements of the copied list ===== ===== Changing elements of the copied list =====
  
-All the below described operations are specific of the way used to create ​the second list.+What it matters here is how the secondary lists were created, and what'​s ​the nature of the changed element (simple or compound).
  
-==== Changing a single ​element ====+==== Changing a simple ​element ====
  
 <code python> <code python>
 # by assignment, it will propagate the changes to the first list # by assignment, it will propagate the changes to the first list
-colors2[0] = "​brown" ​+colors1 = ["​red",​ "​green"​] 
 +colors2 = colors1 
 +colors2[0] = "​brown"​
 colors1, colors2 # ['​brown',​ '​green'​],​ ['​brown',​ '​green'​] colors1, colors2 # ['​brown',​ '​green'​],​ ['​brown',​ '​green'​]
  
 # by slicing, it will be independent # by slicing, it will be independent
-colors3[0] = "​yellow"​ ['brown', '​green'​],​ ['​yellow',​ '​green'​]+colors1 = ["​red",​ "​green"​] 
 +colors3 = colors1[:​] 
 +colors3[0] = "​yellow" ​ 
 +colors1, colors3 # ['red', '​green'​],​ ['​yellow',​ '​green'​]
 </​code>​ </​code>​
  
-==== Changing ​the whole list or sublist ​====+==== Changing ​a compound element ​====
  
-This will create an independent instancefor example:+This is a tricky onebecause no matter the creation method used, the compound element will be always shared.
  
 <code python> <code python>
-colors4 ​colors1 +numbers1 ​[1, 2, [3, 4]] 
-colors4 ​same as colors1, ['​brown'​'​green'​], ['​brown'​'​green'​] +numbers2 = numbers1 
-colors4 = ['​new'​'​colors'​# this will point to a new memory locationtherefore colors1 remains untouched+numbers3 = numbers1[:​] 
 + 
 +numbers3[2][0] = 99 
 +numbers1, numbers2, numbers3 ​[1, 2, [994]], [1, 2, [994]], [1, 2, [99, 4]]
 </​code>​ </​code>​
  
-BUT the object will be shared no matter what if it's a sublist (compound object):+==== Replacing ​the whole list ==== 
 + 
 +Replacing the whole list it will create independent objects, **not sharing** the same memory location anymore.
  
 <code python> <code python>
-numbers1 = [1, 2, [3,4]+numbers1 = [1, 2, 3] 
-numbers2 = numbers1 # it doesn'​t matter if assignment of slicing method is used+numbers2 = numbers1 
 +numbers2 = [4, 5, 6] 
 +numbers1, numbers2 ​[1, 2, 3], [4, 5, 6] 
 +</​code>​
  
 +==== Replacing the whole compound element (sublist) ====
 +
 +This again is tricky, because it matters based on the creation method. As you probably expect, the assignment will still share the same location, but the slicing method will not.
 +
 +<code python>
 +numbers1 = [1, 2, [3, 4]]
 +numbers2 = numbers1
 +numbers3 = numbers1[:]
 +
 +numbers2[2] = [10,20,30] # this will replace also numbers1[2]
 +numbers1, numbers2, numbers3 # [1, 2, [10, 20, 30]], [1, 2, [10, 20, 30]], [1, 2, [3, 4]]
 +
 +numbers3[2] = [100, 200, 300] # but not this one
 +numbers1, numbers2, numbers3 # [1, 2, [10, 20, 30]], [1, 2, [10, 20, 30]], [1, 2, [100, 200, 300]]
 +</​code>​
 +
 +===== Avoiding headaches, making a deep copy =====
 +
 +Use **deepcopy** from **copy** module. Simple as that.
 +
 +<code python>
 +from copy import deepcopy
 +
 +numbers1 = [1, 2, [3, 4]]
 +numbers2 = deepcopy(numbers1) # now the two lists are completely independent
 +</​code>​
python/shallowdeepcopy.txt · Last modified: 2016/01/25 09:08 by admin